字符格式处理

本文介绍了一种字符编码转换的方法,包括从GB2312到Unicode、从Unicode到UTF-8、从UTF-8到Unicode及从Unicode到GB2312的转换过程。通过具体的C/C++代码实现,展示了不同编码间如何进行相互转换。


static void Gb2312_2_Unicode(unsigned short* dst, const char * src)
{
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, 2, (LPWSTR)dst, 1);
}


static void Unicode_2_UTF8(char* dst, unsigned short* src)
{
char* pchar = (char *)src;


dst[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
dst[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
dst[2] = (0x80 | ( pchar[0] & 0x3F));
}


static void UTF8_2_Unicode(unsigned short* dst, const char * src)
{
char* uchar = (char *)dst;


uchar[1] = ((src[0] & 0x0F) << 4) + ((src[1] >> 2) & 0x0F);
uchar[0] = ((src[1] & 0x03) << 6) + (src[2] & 0x3F);
}


static void Unicode_2_GB2312(char* dst, unsigned short uData)
{
WideCharToMultiByte(CP_ACP, NULL, (LPCWSTR)&uData, 1, dst, sizeof(unsigned short), NULL, NULL);
}


static int GB2312_2_UTF8(char * buf, int buf_len, const char * src, int src_len)
{
if (0 == src_len)
src_len = strlen(src);


int j = 0;
for (int i = 0; i < src_len;)
{
if (j >= buf_len - 1)
break;


if (src[i] >= 0)
{
buf[j++] = src[i++];
}
else
{
unsigned short w_c = 0;
Gb2312_2_Unicode(&w_c, src + i);


char tmp[4] = "";
Unicode_2_UTF8(tmp, &w_c);


buf[j+0] = tmp[0];
buf[j+1] = tmp[1];
buf[j+2] = tmp[2];


i += 2;
j += 3;
}
}


buf[j] = '\0';


return j;
}


static int UTF8_2_GB2312(char * buf, int buf_len, const char * src, int src_len)
{
if (0 == src_len)
src_len = strlen(src);


int j = 0;
for (int i = 0; i < src_len;)
{
if (j >= buf_len - 1)
break;


if (src[i] >= 0)
{
buf[j++] = src[i++];
}
else
{
unsigned short w_c = 0;
UTF8_2_Unicode(&w_c, src + i);


char tmp[4] = "";
Unicode_2_GB2312(tmp, w_c);


buf[j+0] = tmp[0];
buf[j+1] = tmp[1];


i += 3;
j += 2;
}
}


buf[j] = '\0';


return j;
}

转载于:https://my.oschina.net/u/262868/blog/113657

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值