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


    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);
      }

  • 相关阅读:
    认识ASP.NET 中的 AppDomain
    试验总结1 改变递归函数中的执行内容
    试验总结2 break与continue
    开篇的话
    01复杂度3 二分查找
    02线性结构2 一元多项式的乘法与加法运算
    01复杂度2 Maximum Subsequence Sum
    02线性结构4 Pop Sequence
    01复杂度1 最大子列和问题
    02线性结构1 两个有序链表序列的合并
  • 原文地址:https://www.cnblogs.com/rain2012qf/p/3979116.html
Copyright © 2020-2023  润新知