UTF到UNICODE编码转换

1608阅读 0评论2011-09-03 zpf1218
分类:C/C++

  1. inline bool UTF2UNICODE(const char* src, CString &t)
  2. {
  3.     if (src == NULL)
  4.     {
  5.         ASSERT(FALSE);
  6.         return false;
  7.     }

  8.     int size_s = (int)strlen(src);
  9.     int size_d = size_s + 10; //?

  10.     wchar_t *des = new wchar_t[size_d];
  11.     memset(des, 0, size_d * sizeof(wchar_t));

  12.     int s = 0, d = 0;
  13.     bool toomuchbyte = true; //set true to skip error prefix.

  14.     while (s < size_s && d < size_d)
  15.     {
  16.         unsigned char c = src[s];
  17.         if ((c & 0x80) == 0)
  18.         {
  19.             des[d++] += src[s++];
  20.         }
  21.         else if((c & 0xE0) == 0xC0) ///< 110x-xxxx 10xx-xxxx
  22.         {
  23.             WCHAR &wideChar = des[d++];
  24.             wideChar = (src[s + 0] & 0x3F) << 6;
  25.             wideChar |= (src[s + 1] & 0x3F);

  26.             s += 2;
  27.         }
  28.         else if((c & 0xF0) == 0xE0) ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
  29.         {
  30.             WCHAR &wideChar = des[d++];

  31.             wideChar = (src[s + 0] & 0x1F) << 12;
  32.             wideChar |= (src[s + 1] & 0x3F) << 6;
  33.             wideChar |= (src[s + 2] & 0x3F);

  34.             s += 3;
  35.         }
  36.         else if((c & 0xF8) == 0xF0) ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx
  37.         {
  38.             WCHAR &wideChar = des[d++];

  39.             wideChar = (src[s + 0] & 0x0F) << 18;
  40.             wideChar = (src[s + 1] & 0x3F) << 12;
  41.             wideChar |= (src[s + 2] & 0x3F) << 6;
  42.             wideChar |= (src[s + 3] & 0x3F);

  43.             s += 4;
  44.         }
  45.         else
  46.         {
  47.             WCHAR &wideChar = des[d++]; ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx

  48.             wideChar = (src[s + 0] & 0x07) << 24;
  49.             wideChar = (src[s + 1] & 0x3F) << 18;
  50.             wideChar = (src[s + 2] & 0x3F) << 12;
  51.             wideChar |= (src[s + 3] & 0x3F) << 6;
  52.             wideChar |= (src[s + 4] & 0x3F);

  53.             s += 5;
  54.         }
  55.     }

  56.     t = des;
  57.     delete[] des;
  58.     des = NULL;

  59.     return true;
  60. }
上一篇:HeapAlloc、VirtualAlloc、GlobalAlloc和LocalAlloc
下一篇:chrome中的thread 和SimpleThread的区别