Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88263
  • 博文数量: 18
  • 博客积分: 510
  • 博客等级: 中士
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-27 20:37
文章分类

全部博文(18)

文章存档

2011年(18)

分类: C/C++

2011-04-27 17:00:11

  1. UTF-8: 3字节一个字符
  2. UNICODE: 2字节一个字符
  3. GB2312: 1字节一个字符

  4. 例子:

  5. “你”字的UTF-8编码: E4 BD A0        11100100 10111101 10100000
  6. “你”的Unicode编码: 4F 60          01001111 01100000
  7. 按照UTF-8的编码规则,分解如下:xxxx0100 xx111101 xx100000
  8. 把除了x之外的数字拼接在一起,就变成“你”的Unicode编码了。
  9. 注意UTF-8的最前面3个1,表示整个UTF-8串是由3个字节构成的。
  10. 经过UTF-8编码之后,再也不会出现敏感字符了,因为最高位始终为1。

  11. 类定义
  12. class CChineseCode
  13. {
  14.    public:
  15.        static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
  16.        static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
  17.        static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
  18.        static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
  19.        static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
  20.        static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
  21. };

  22. 类实现

  23. void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
  24. {
  25.     char* uchar = (char *)pOut;

  26.     uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
  27.     uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);

  28.     return;
  29. }

  30. void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
  31. {
  32.     // 注意 WCHAR高低字的顺序,低字节在前,高字节在后
  33.     char* pchar = (char *)pText;

  34.     pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  35.     pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  36.     pOut[2] = (0x80 | (pchar[0] & 0x3F));

  37.     return;
  38. }

  39. void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
  40. {
  41.     WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
  42.     return;
  43. }

  44. void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
  45. {
  46.     ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
  47.     return ;
  48. }

  49. void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
  50. {
  51.     char buf[4];
  52.     int nLength = pLen* 3;
  53.     char* rst = new char[nLength];
  54.     
  55.     memset(buf,0,4);
  56.     memset(rst,0,nLength);
  57.     
  58.     int i = 0;
  59.     int j = 0;
  60.     while(i < pLen)
  61.     {
  62.             //如果是英文直接复制就可以
  63.             if( *(pText + i) >= 0)
  64.             {
  65.                     rst[j++] = pText[i++];
  66.             }
  67.             else
  68.             {
  69.                     wchar_t pbuffer;
  70.                     Gb2312ToUnicode(&pbuffer,pText+i);
  71.                     
  72.                     UnicodeToUTF_8(buf,&pbuffer);
  73.                     
  74.                     unsigned short int tmp = 0;
  75.                     tmp = rst[j] = buf[0];
  76.                     tmp = rst[j+1] = buf[1];
  77.                     tmp = rst[j+2] = buf[2];
  78.                     
  79.                     j += 3;
  80.                     i += 2;
  81.             }
  82.     }
  83.     rst[j] = '\0';

  84.     //返回结果
  85.     pOut = rst;
  86.     delete []rst;
  87.     
  88.     return;
  89. }

  90. void CChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
  91. {
  92.     char * newBuf = new char[pLen];
  93.     char Ctemp[4];
  94.     memset(Ctemp,0,4);

  95.     int i =0;
  96.     int j = 0;
  97.     
  98.     while(i < pLen)
  99.     {
  100.         if(pText[i] > 0)
  101.         {
  102.                 newBuf[j++] = pText[i++];
  103.         }
  104.         else
  105.         {
  106.                 WCHAR Wtemp;
  107.                 UTF_8ToUnicode(&Wtemp,pText + i);
  108.         
  109.                 UnicodeToGB2312(Ctemp,Wtemp);
  110.             
  111.                 newBuf[j] = Ctemp[0];
  112.                 newBuf[j + 1] = Ctemp[1];

  113.                 i += 3;
  114.                 j += 2;
  115.         }
  116.     }
  117.     newBuf[j] = '\0';
  118.     
  119.     pOut = newBuf;
  120.     delete []newBuf;
  121.     
  122.     return;
  123. }
阅读(840) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~