zoukankan      html  css  js  c++  java
  • mfc字符转码

    std::wstring UTF8ToUnicode(const std::string& utf8string)

     1 {
     2  int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
     3  if (widesize == ERROR_NO_UNICODE_TRANSLATION)
     4  {
     5   throw std::exception("Invalid UTF-8 sequence.");
     6  }
     7  if (widesize == 0)
     8  {
     9   throw std::exception("Error in conversion.");
    10  }
    11  std::vector<wchar_t> resultstring(widesize);
    12  int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
    13  if (convresult != widesize)
    14  {
    15   throw std::exception("La falla!");
    16  }
    17  return std::wstring(&resultstring[0]);
    18 }
    View Code

    std::string WideByteToAcsi(std::wstring& wstrcode)

     1 {
     2  int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
     3  if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
     4  {
     5   throw std::exception("Invalid UTF-8 sequence.");
     6  }
     7  if (asciisize == 0)
     8  {
     9   throw std::exception("Error in conversion.");
    10  }
    11  std::vector<char> resultstring(asciisize);
    12  int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
    13  if (convresult != asciisize)
    14  {
    15   throw std::exception("La falla!");
    16  }
    17  return std::string(&resultstring[0]);
    18 }
    View Code


    std::string UTF8ToASCII(std::string& strUtf8Code)

    {
     std::string strRet("");
     //先把 utf8 转为 unicode   
     std::wstring wstr = UTF8ToUnicode(strUtf8Code);
     //最后把 unicode 转为 ascii   
     strRet = WideByteToAcsi(wstr);
     return strRet;
    }
    View Code

    std::string ASCIIToUTF8(std::string& strASCIICode)

     1 {
     2  std::string strOutUTF8 = "";
     3  WCHAR * str1;
     4  int n = MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, NULL, 0);
     5  str1 = new WCHAR[n];
     6  MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, str1, n);
     7  n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
     8  char * str2 = new char[n];
     9  WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    10  strOutUTF8 = str2;
    11  delete[]str1;
    12  str1 = NULL;
    13  delete[]str2;
    14  str2 = NULL;
    15  return strOutUTF8;
    16 }
    View Code

    //!!为了减少字符串的拷贝,改为使用shared_array,性能升级

    #include <boost/shared_array.hpp>

    #define CHAR_EMPTY_ARR_PTR boost::shared_array<char>(new char[1]{''})
    #define WCHAR_EMPTY_ARR_PTR boost::shared_array<wchar_t>(new wchar_t[1]{L''})

    boost::shared_array<char> UTF8ToASCII(std::string& strUtf8Code)

    1 {
    2  return std::move(UTF8ToASCII(strUtf8Code.c_str()));
    3 }
    View Code

    boost::shared_array<char> UTF8ToASCII(const char* ch, int nLen /*= -1*/)

    1 {
    2  //先把 utf8 转为 unicode   
    3  auto wstr = UTF8ToUnicode(ch, nLen);
    4  //最后把 unicode 转为 ascii   
    5  return std::move(WideByteToAcsi(wstr.get()));
    6 }
    View Code

    boost::shared_array<char> ASCIIToUTF8(std::string& strASCIICode)

    1 {
    2  return std::move(ASCIIToUTF8(strASCIICode.c_str()));
    3 }
    View Code

    boost::shared_array<char> ASCIIToUTF8(const char* ch, int nLen/* = -1*/)

     1 {
     2  int n = MultiByteToWideChar(CP_ACP, 0, ch, nLen, NULL, 0);
     3  if (0 == n)
     4  {
     5   return CHAR_EMPTY_ARR_PTR;
     6  }
     7  boost::shared_array<wchar_t> ptrWtArr(new wchar_t[n]{ L'' });
     8  if (0 == MultiByteToWideChar(CP_ACP, 0, ch, nLen, ptrWtArr.get(), n))
     9  {
    10   return CHAR_EMPTY_ARR_PTR;
    11  }
    12  n = WideCharToMultiByte(CP_UTF8, 0, ptrWtArr.get(), -1, NULL, 0, NULL, NULL);
    13  if (0 == n)
    14  {
    15   return CHAR_EMPTY_ARR_PTR;
    16  }
    17  boost::shared_array<char> ptrArr(new char[n]{ '' });
    18  if (0 == WideCharToMultiByte(CP_UTF8, 0, ptrWtArr.get(), -1, ptrArr.get(), n, NULL, NULL))
    19  {
    20   return CHAR_EMPTY_ARR_PTR;
    21  }
    22  return std::move(ptrArr);
    23 }
    View Code

    boost::shared_array<wchar_t> UTF8ToUnicode(const char* ch, int nLen /*= -1*/)

     1 {
     2  int widesize = ::MultiByteToWideChar(CP_UTF8, 0, ch, nLen, NULL, 0);
     3  if (0 == widesize)
     4  {
     5   return WCHAR_EMPTY_ARR_PTR;
     6  }
     7  boost::shared_array<wchar_t> ptrWtArr(new wchar_t[widesize]{ L'' });
     8  int convresult = ::MultiByteToWideChar(CP_UTF8, 0, ch, nLen, ptrWtArr.get(), widesize);
     9  if (0 == convresult)
    10  {
    11   return WCHAR_EMPTY_ARR_PTR;
    12  }
    13  return std::move(ptrWtArr);
    14 }
    15 boost::shared_array<char> DPC::WideByteToAcsi(std::wstring& wstrcode)
    16 {
    17  return std::move(WideByteToAcsi(wstrcode.c_str()));
    18 }
    View Code

    boost::shared_array<char> WideByteToAcsi(const wchar_t* wch, int nLen /*= -1*/)

     1 {
     2  int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wch, nLen, NULL, 0, NULL, NULL);
     3  if (0 == asciisize)
     4  {
     5   return CHAR_EMPTY_ARR_PTR;
     6  }
     7  boost::shared_array<char> ptrArr(new char[asciisize + 1]{''});
     8  int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wch, nLen, ptrArr.get(), asciisize, NULL, NULL);
     9  if (0 == convresult)
    10  {
    11   return CHAR_EMPTY_ARR_PTR;
    12  }
    13  return std::move(ptrArr);
    14 }
    View Code
  • 相关阅读:
    【网络文摘】一位36岁程序员的困惑
    【网络文摘】大龄程序员怎样渡过中年危机?
    【问题与解决】showModalDialog is not defined 的解决方案
    【ASP.NET 问题】ASP.NET 网站404页面返回200,或者302的解决办法
    IIS 网站 HTTP 转 HTTPS
    ECharts JS应用:图表页面实现
    【问题与解决】怎么删除TFS云端上的项目
    【问题与解决】Github 上传代码报错(error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version)
    【问题与解决】Mac OS通过 npm 安装 React Native 报错(checkPermissions Missing write access to /usr/local/lib/node_modules)
    【JavaScript 插件】图片展示插件 PhotoSwipe 初识
  • 原文地址:https://www.cnblogs.com/osbreak/p/9207869.html
Copyright © 2011-2022 走看看