zoukankan      html  css  js  c++  java
  • 字符编码转换(使用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);
      }

  • 相关阅读:
    jQuery jsonp跨域请求
    Solr——使用edismax控制评分
    Solr——评分公式修改
    Solr——自定义评分组件
    Jmeter——添加参数的四种方法
    数据挖掘——Data competition: From 0 to 1: Part I
    数据分析——Hive数据库初始化失败Error: FUNCTION 'NUCLEUS_ASCII' already exists.
    Python——anaconda下的jupyter找不到pip下载的模块
    数据分析——5天破10亿的哪吒,为啥这么火,Python来分析
    数据分析——巧用ABtest,看杰伦和徐坤的流量之争
  • 原文地址:https://www.cnblogs.com/rain2012qf/p/3979116.html
Copyright © 2011-2022 走看看