Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1106548
  • 博文数量: 143
  • 博客积分: 969
  • 博客等级: 准尉
  • 技术积分: 1765
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-30 12:09
文章分类

全部博文(143)

文章存档

2023年(4)

2021年(2)

2020年(4)

2019年(4)

2018年(33)

2017年(6)

2016年(13)

2014年(7)

2013年(23)

2012年(33)

2011年(14)

我的朋友

分类: C/C++

2011-09-03 11:55:35

  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. }
阅读(1503) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~