- UID
- 1656
- 麦力
- 10614
- 注册时间
- 2016-11-7
- 最后登录
- 2023-7-14
- 精华
- 8
- 阅读权限
- 80
- 在线时间
- 1040 小时
|
从API得到 \u3010\u5df2\u7b7e\u6536\uff0c\u7b7e\u6536\u4eba\u662f\u5feb\u9012\u9a7f\u7ad9\u3011这样的字符串,要转成utf8
下面哪有问题呢?
static int hex2int(char c)
{
return (c >= 'a' ? (10 + c - 'a') : (c - '0'));
}
static char *unicode2utf8(char *str)
{
// \u3010\u5df2\u7b7e\u6536\uff0c\u7b7e\u6536\u4eba\u662f\u5feb\u9012\u9a7f\u7ad9\u3011
char utf8[3], *out;
int i, j = strlen(str);
wchar_t ch;
out = (char *)malloc(200);
for (i = 0; 6*i < j; i++)
{
ch = hex2int(*(str + 6 * i + 2)) << 12 + hex2int(*(str + 6 * i + 3)) << 8 + hex2int(*(str + 6 * i + 4)) << 4 + hex2int(*(str + 6 * i + 5));
memset(utf8, 0, 3);
utf8[0] = 0xE0 | (ch >> 12);
utf8[1] = 0x80 | ((ch >> 6) & 0x3F);
utf8[2] = 0x80 | (ch & 0x3F);
strcat(out, utf8);
}
return out;
}
|
|