广交好友~~ 想要讨论的可以留下msn~~~
希望群友网友经常能提出问题,一起解决,共同提高
UTF-8的unicode表示方法到unicode的值转换函数
上一篇 /
下一篇 2010-10-13 11:39:59
/ 个人分类:国际化
UTF-8的unicode表示方法到unicode的值转换
函数bool utf82unicode(unsigned int byte[], int index, int count, int& unicode)
{
/* for (int i=index; i < count; ++i) {
printf("byte[%d]:%0Xn",i, byte
);
}
printf("byte[index] & 0x80: %0Xn", byte[index] & 0x80);
printf("byte[index] & 0xE0: %0Xn", byte[index] & 0xE0);
printf("byte[index] & 0xF0: %0Xn", byte[index] & 0xF0);
*/
if (index >= count) return false;
if ( (byte[index] & 0x80) == 0x0) // 一位
{
unicode = byte[index];
}
else if ((byte[index] & 0xE0) == 0xC0) // 两位
{
if (index + 1 >= count ) return false;
unicode = (((int)(byte[index] & 0x1F)) << 6)
| (byte[ index + 1] & 0x3F);
}
else if ((byte[index] & 0xF0) == 0xE0) // 三位
{
if (index + 2 >= count) return false;
unicode = (((int)(byte[index] & 0x0F)) << 12)
| (((int)(byte[index + 1] & 0x3F)) << 6)
| (byte[index + 2] & 0x3F);
}
else if ((byte[index] & 0xF8) == 0xF0) // 四位
{
if (index + 3 >= count) return false;
unicode = (((int)(byte[index] & 0x07)) << 18)
| (((int)(byte[index + 1] & 0x3F)) << 12)
收藏
举报
TAG: