unicode 转 utf8
2017-07-12 16:07
148 查看
int unicode_to_utf8(uint16 *in, int insize, unsigned char **out)
{
int i = 0;
int outsize = 0;
int charscount = 0;
unsigned char *result = NULL;
unsigned char *tmp = NULL;
charscount = insize;
result = (unsigned char*)malloc(charscount * 3 + 1);
memset(result, 0, charscount * 3 + 1);
tmp = result;
for (i = 0; i < charscount; i++)
{
uint16 unicode = in[i];
if (unicode >= 0x0000 && unicode <= 0x007f)
{
*tmp = (char)unicode;
tmp += 1;
outsize += 1;
}
else if (unicode >= 0x0080 && unicode <= 0x07ff)
{
*tmp = 0xc0 | (unicode >> 6);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 2;
}
else if (unicode >= 0x0800 && unicode <= 0xffff)
{
*tmp = 0xe0 | (unicode >> 12);
tmp += 1;
*tmp = 0x80 | (unicode >> 6 & 0x003f);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 3;
}
}
*tmp = '\0';
*out = result;
return 0;
}
{
int i = 0;
int outsize = 0;
int charscount = 0;
unsigned char *result = NULL;
unsigned char *tmp = NULL;
charscount = insize;
result = (unsigned char*)malloc(charscount * 3 + 1);
memset(result, 0, charscount * 3 + 1);
tmp = result;
for (i = 0; i < charscount; i++)
{
uint16 unicode = in[i];
if (unicode >= 0x0000 && unicode <= 0x007f)
{
*tmp = (char)unicode;
tmp += 1;
outsize += 1;
}
else if (unicode >= 0x0080 && unicode <= 0x07ff)
{
*tmp = 0xc0 | (unicode >> 6);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 2;
}
else if (unicode >= 0x0800 && unicode <= 0xffff)
{
*tmp = 0xe0 | (unicode >> 12);
tmp += 1;
*tmp = 0x80 | (unicode >> 6 & 0x003f);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 3;
}
}
*tmp = '\0';
*out = result;
return 0;
}
相关文章推荐
- unicode和utf8的区别
- c++ ANSI、UNICODE、UTF8互转
- utf8转Unicode
- python 中文编码(unicode,gbk,utf8,ordinary string)
- unicode、utf8、字符串字面值
- utf8和unicode编码究竟是什么关系?有何区别?
- UTF8, ASCII, UNICODE相互转换
- MFC Cstring unicode 转换为utf8
- MYSQL utf8_unicode_ci 和utf8_general_ci的区别
- Ansi,UTF8,Unicode,ASCII编码的区别
- 字符集GBK, Unicode, 编码GBK, Unicode, UTF8, UTF16
- unicode和utf8的关系
- 彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, UTF8.(GBK, GB18030, BIG5, UTF-7,UTF-16,UTF-32)
- 【转】Mysql中校对集utf8_unicode_ci与utf8_general_ci的区别
- python中unicode、utf8、gbk等编码问题
- 跨平台utf8转unicode研究实现(2)
- 20091006 - MySQL 中校对集 utf8_unicode_ci 与 utf8_general_ci 的区别
- ansi-unicode-utf8
- 又一篇unicode ,ansi ,utf8
- Mysql中校对集utf8_unicode_ci与utf8_general_ci的区别