Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350396
  • 博文数量: 89
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 951
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-23 11:51
个人简介

好懒,什么都没写

文章分类

全部博文(89)

文章存档

2012年(3)

2011年(17)

2007年(20)

2006年(49)

我的朋友

分类: C/C++

2011-05-31 11:00:05

1.多字节转换为Unicode
  1. bool multiToUnicode(const std::string& multiText, std::wstring& unicodeText)
  2. {
  3.         
  4.     int size = ::MultiByteToWideChar(CP_ACP,
  5.                     0,
  6.                     multiText.c_str(),
  7.                     -1,
  8.                     NULL,
  9.                      0);
  10.     if (0 == size)
  11.     {
  12.         return false;
  13.     }

  14.     wchar_t* wszBuffer = new wchar_t[size + 1];
  15.     ::ZeroMemory(wszBuffer, (size + 1) * sizeof(wchar_t));

  16.     if (0 == ::MultiByteToWideChar(CP_ACP,
  17.                     0,
  18.                      multiText.c_str(),
  19.                      -1,
  20.                     wszBuffer,
  21.                     size + 1))
  22.     {
  23.         delete[] wszBuffer;
  24.         return false;
  25.     }
  26.         
  27.     unicodeText = wszBuffer;
  28.     delete[] wszBuffer;
  29.     return true;

  30. }

 

 

2.多字节转换为 UTF-8

分两步走:

   第一步:将多字节转换为 Unicode;

   第二步,将Unicode转换为 UTF-8

 

  1. bool multiToUTF8(const std::string& multiText, std::string& utf8Text)
  2. {
  3.         
  4.     // 把输入转换为Unicode
  5.     int size = ::MultiByteToWideChar(CP_ACP,
  6.                      0,
  7.                      multiText.c_str(),
  8.                     -1,
  9.                     NULL,
  10.                     0);
  11.     if (0 == size)
  12.     {
  13.         return false;
  14.     }

  15.     wchar_t* wszBuffer = new wchar_t[size + 1];
  16.     ::ZeroMemory(wszBuffer, (size + 1) * sizeof(wchar_t));

  17.     if (0 == ::MultiByteToWideChar(CP_ACP,
  18.                     0,
  19.                     multiText.c_str(),
  20.                     -1,
  21.                     wszBuffer,
  22.                     size + 1))
  23.     {
  24.         delete[] wszBuffer;
  25.         return false;
  26.     }
  27.     
  28.     // Unicode->UTF8的转换
  29.     size = ::WideCharToMultiByte(CP_UTF8,
  30.                      0,
  31.                     wszBuffer,
  32.                     -1,
  33.                     NULL,
  34.                     0,
  35.                     NULL,
  36.                     NULL);
  37.     if (0 == size)
  38.     {
  39.         delete[] wszBuffer;
  40.         return false;
  41.     }

  42.     char* szBuffer = new char[size + 1];
  43.     ::ZeroMemory(szBuffer, (size + 1) * sizeof(char));

  44.     if (0 == ::WideCharToMultiByte(CP_UTF8,
  45.                      0,
  46.                     wszBuffer,
  47.                     -1,
  48.                     szBuffer,
  49.                     size + 1,
  50.                     NULL,
  51.                     NULL))
  52.     {
  53.         delete[] wszBuffer;
  54.         delete[] szBuffer;
  55.         return false;
  56.     }

  57.     utf8Text = szBuffer;
  58.     delete[] wszBuffer;
  59.     delete[] szBuffer;
  60.     return true;
  61. }

 

 

阅读(7792) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~