2019-07-21 08:13发布
电子之旅 发表于 2016-11-10 19:31 先将UTF8转Unicode然后再转GBK,汉字显示实验就是使用Unicode转GBK。UTF8转Unicode代码度娘一大把
电子之旅 发表于 2016-11-15 14:49 [mw_shl_code=c,true]/* convert UTF-8 to unicode */ u8 UTF8toUnicode(u8 *ch, u16 *_unicode) {
最多设置5个标签!
int SwitchToGbk(const unsigned char* pszBufIn, int nBufInLen, unsigned char* pszBufOut, int* pnBufOutLen)
{
int i = 0;
int j = 0, nLen;
unsigned short unicode;
unsigned short gbk;
for(; i < nBufInLen; i++, j++)
{
if((pszBufIn[i] & 0x80) == 0x00) // 1位
{
nLen = 1;
pszBufOut[j]= pszBufIn[i];
}
/*
else if((pszBufIn[i] & 0xE0) == 0xC0)// 2位
{
nLen = 2;
unicode = (pszBufIn[i] & 0x1F << 6) | (pszBufIn[i+1]& 0x3F);
}*/
else if ((pszBufIn[i] & 0xF0) == 0xE0) // 3位
{
if (i+ 2 >= nBufInLen) return -1;
unicode = (((int)(pszBufIn[i] & 0x0F)) << 12) | (((int)(pszBufIn[i+1] & 0x3F)) << 6) | (pszBufIn[i+2] & 0x3F);
gbk = mb_uni2gb_table[unicode-0x4e00];
pszBufOut[j]= gbk/256;
pszBufOut[j+1] = gbk%256;
j++;
i+=2;
}
else
{
return -1;
}
}
*pnBufOutLen = j;
printf(stderr,"pnbufoutlen=%d ",*pnBufOutLen);
return 0;
}
网上的都是基于操作系统的函数,单片机不能调用的
[mw_shl_code=c,true]/* convert UTF-8 to unicode */
u8 UTF8toUnicode(u8 *ch, u16 *_unicode)
{
u8 *p = NULL ,n = 0;
u16 e = 0;
p = ch;
if(1)//p == NULL
{
if(*p >= 0xfc)
{
/*6:<11111100>*/
e = (p[0] & 0x01) << 30;
e |= (p[1] & 0x3f) << 24;
e |= (p[2] & 0x3f) << 18;
e |= (p[3] & 0x3f) << 12;
e |= (p[4] & 0x3f) << 6;
e |= (p[5] & 0x3f);
n = 6;
}
else if(*p >= 0xf8)
{
/*5:<11111000>*/
e = (p[0] & 0x03) << 24;
e |= (p[1] & 0x3f) << 18;
e |= (p[2] & 0x3f) << 12;
e |= (p[3] & 0x3f) << 6;
e |= (p[4] & 0x3f);
n = 5;
}
else if(*p >= 0xf0)
{
/*4:<11110000>*/
e = (p[0] & 0x07) << 18;
e |= (p[1] & 0x3f) << 12;
e |= (p[2] & 0x3f) << 6;
e |= (p[3] & 0x3f);
n = 4;
}
else if(*p >= 0xe0)
{
/*3:<11100000>*/
e = (p[0] & 0x0f) << 12;
e |= (p[1] & 0x3f) << 6;
e |= (p[2] & 0x3f);
n = 3;
}
else if(*p >= 0xc0)
{
/*2:<11000000>*/
e = (p[0] & 0x1f) << 6;
e |= (p[1] & 0x3f);
n = 2;
}
else
{
e = p[0];
n = 1;
}
*_unicode = e;
}
return n;
}[/mw_shl_code]
多谢大神,我试试看
一周热门 更多>