为了节省网络带宽,很多网络应用程序都是使用了UTF8编码方式,比如MSN等,UTF8的编码原理其实很简单,这里给一段Windows下面的VC实例。
#define UNKOWN_CHAR '?'
#define isByteOfOneByteChar(b) (((b) & 0x80) == 0x00)
#define isHeadOfTwoBytesChar(b) (((b) & 0xE0) == 0xC0)
#define isHeadOfThreeBytesChar(b) (((b) & 0xF0) == 0xE0)
#define isComingByteOfMultBytesChar(b) (((b) & 0xC0)==0x80)
#define isHeadOfUTF8Char(b) isByteOfOneByteChar(b) || isByteOfOneByteChar(b) || isHeadOfThreeBytesChar(b)
Data Data::UTF8Decode(const Data &utf8hex)
{
int L = utf8hex.length();
unsigned short* unicode = new unsigned short[L * 2];
int outi = 0;
for(int i = 0; i < L - 1;) //1-
{
BYTE b1 =(BYTE)utf8hex.getChar(i);
if(isByteOfOneByteChar(b1))
{
unicode[outi++] = b1;
i++;
}
else if(isHeadOfTwoBytesChar(b1))
{
if(i + 2 > L)
{
unicode[outi++] = UNKOWN_CHAR;
break;
}
BYTE b2 =(BYTE)utf8hex.getChar(i + 1);
if(isComingByteOfMultBytesChar(b2))
{
unsigned short tmp = b1 & 0x1F;
tmp = (tmp << 6) + (b2 & 0x3F);
unicode[outi++] = tmp;
i += 2;
}
else if(isHeadOfUTF8Char(b2))
{
unicode[outi++] = UNKOWN_CHAR;
i += 1;
}else
{
unicode[outi++] = UNKOWN_CHAR;
i +=2;
}
}
else if(isHeadOfThreeBytesChar(b1))
{
if(i + 2 > L)
{
unicode[outi++] = UNKOWN_CHAR;
break;
}
else if(i + 3 > L)
{
BYTE b2 =(BYTE)utf8hex.getChar(i + 1);
if(isByteOfOneByteChar(b2))
{
unicode[outi++] = UNKOWN_CHAR;
i += 1;
continue;
}else
{
unicode[outi++] = UNKOWN_CHAR;
break;
}
}
BYTE b2 =(BYTE)utf8hex.getChar(i + 1);
BYTE b3 =(BYTE)utf8hex.getChar(i + 2);
if(isComingByteOfMultBytesChar(b2) && isComingByteOfMultBytesChar(b3))
{
unsigned short tmp = b1 & 0x0F;
tmp = tmp << 12;
unsigned short tmp2 = b2 & 0x3F;
tmp2 = tmp2 << 6;
unicode[outi++] = tmp + tmp2 + (b3 & 0x3F);
i +=3;
}
else if(isHeadOfUTF8Char(b2))
{
unicode[outi++] = UNKOWN_CHAR;
i += 1;
}else if(isHeadOfUTF8Char(b3))
{
unicode[outi++] = UNKOWN_CHAR;
i += 2;
}else
{
unicode[outi++] = UNKOWN_CHAR;
i +=3;
}
}else
{
unicode[outi++] = UNKOWN_CHAR;
i += 1;
}
}
unicode[outi] = 0;
Data ret(outi * 4, true);
#ifdef WIN32
int count = WideCharToMultiByte(0, 0, unicode, outi, ret.mBuf, outi * 4, NULL, NULL);
if(count == 0)
cpLog(LOG_ERR, "Get UTF byte, error number=%lu", GetLastError());
#else
#endif
if(count > 0)
{
ret.mBuf[count] = '\0';
ret.mLength = count;
}
delete [] unicode;
return ret;
}