码迷,mamicode.com
首页 > Windows程序 > 详细

字符编码转换(使用windows api)

时间:2014-09-18 14:48:43      阅读:431      评论:0      收藏:0      [点我收藏+]

标签:des   使用   ar   art   sp   on   c   amp   size   

static std::wstring Utf8ToWString(const std::string& sText)
  {
   int nLenWideCharStr = MultiByteToWideChar(CP_UTF8, 0, sText.c_str(), -1, NULL, 0);
   PWCHAR pWideCharStr = NULL;
   pWideCharStr =(PWSTR)HeapAlloc(GetProcessHeap(), 0, nLenWideCharStr*sizeof(wchar_t));
   memset(pWideCharStr,0,nLenWideCharStr*sizeof(wchar_t));
   MultiByteToWideChar(CP_UTF8, 0, sText.c_str(), -1, pWideCharStr, nLenWideCharStr);
   std::wstring wideStr = pWideCharStr;
   HeapFree(GetProcessHeap(), 0, pWideCharStr);
   return wideStr;
  }

  static std::string WStringToUtf8(const std::wstring& sText)
  {
   int nLenWideCharStr = WideCharToMultiByte(CP_UTF8, 0, sText.c_str(), -1, NULL, 0 , NULL, NULL);
   PCHAR pCharStr = NULL;
   pCharStr =(PSTR)HeapAlloc(GetProcessHeap(), 0, nLenWideCharStr*sizeof(char));
   memset(pCharStr,0,nLenWideCharStr*sizeof(char));
   WideCharToMultiByte(CP_UTF8, 0, sText.c_str(), -1, pCharStr, nLenWideCharStr, NULL, NULL);
   std::string str = pCharStr;
   HeapFree(GetProcessHeap(), 0, pCharStr);
   return str;
  }

  static std::string WStringToANSI(const std::wstring& sText)
  {
   int nLenWideCharStr = WideCharToMultiByte(CP_ACP, 0, sText.c_str(), -1, NULL, 0 , NULL, NULL);
   PCHAR pCharStr = NULL;
   pCharStr =(PSTR)HeapAlloc(GetProcessHeap(), 0, nLenWideCharStr*sizeof(char));
   memset(pCharStr,0,nLenWideCharStr*sizeof(char));
   WideCharToMultiByte(CP_ACP, 0, sText.c_str(), -1, pCharStr, nLenWideCharStr, NULL, NULL);
   std::string str = pCharStr;
   HeapFree(GetProcessHeap(), 0, pCharStr);
   return str;
  }

  static std::wstring ANSIToWString(const std::string& sText)
  {
   int nLenWideCharStr = MultiByteToWideChar(CP_ACP, 0, sText.c_str(), -1, NULL, 0);
   PWCHAR pWideCharStr = NULL;
   pWideCharStr =(PWSTR)HeapAlloc(GetProcessHeap(), 0, nLenWideCharStr*sizeof(wchar_t));
   memset(pWideCharStr,0,nLenWideCharStr*sizeof(wchar_t));
   MultiByteToWideChar(CP_ACP, 0, sText.c_str(), -1, pWideCharStr, nLenWideCharStr);
   std::wstring wideStr = pWideCharStr;
   HeapFree(GetProcessHeap(), 0, pWideCharStr);
   return wideStr;

  }

  static std::string Utf8ToAnsi(const std::string& sText)
  {
   std::wstring ws = Utf8ToWString(sText);
   return WStringToANSI(ws);
  }

  static std::string AnsiToUtf8(const std::string& sText)
  {
   std::wstring ws = ANSIToWString(sText);
   return WStringToUtf8(ws);
  }

字符编码转换(使用windows api)

标签:des   使用   ar   art   sp   on   c   amp   size   

原文地址:http://www.cnblogs.com/rain2012qf/p/3979116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!