zoukankan      html  css  js  c++  java
  • window下 ANSI Unicode utf8之间相互转换

    //CString to string的转可能只适用于某些编码情况
    string CStringToChar(const CString& strSrc)
    {
      CString strTmp(strSrc);
      string str(strTmp.GetBuffer(strTmp.GetLength()));
      strTmp.ReleaseBuffer();
      return str;
    }

    wstring AnsiToUnicode(const string& buf)
    {
      int len = ::MultiByteToWideChar(CP_ACP, 0, buf.c_str(), -1, NULL, 0);
      if (len == 0) return L"";

      wstring strUnicode;
      strUnicode.resize(len);
      ::MultiByteToWideChar(CP_ACP, 0, buf.c_str(), -1, (wchar_t*)strUnicode.c_str(), len);

      return strUnicode;
    }

    string UnicodeToAnsi(const wstring& buf)
    {
      int len = ::WideCharToMultiByte(CP_ACP, 0, buf.c_str(), -1, NULL, 0, NULL, NULL);
      if (len == 0) return "";

      string strUtf8;
      strUtf8.resize(len);
      ::WideCharToMultiByte(CP_ACP, 0, buf.c_str(), -1, (char*)strUtf8.c_str(), len, NULL, NULL);

      return strUtf8;
    }

    wstring Utf8ToUnicode(const string& buf)
    {
      int len = ::MultiByteToWideChar(CP_UTF8, 0, buf.c_str(), -1, NULL, 0);
      if (len == 0) return L"";

      wstring strUnicode;
      strUnicode.resize(len);
      ::MultiByteToWideChar(CP_UTF8, 0, buf.c_str(), -1, (wchar_t*)strUnicode.c_str(), len);

      return strUnicode;
    }

    string UnicodeToUtf8(const wstring& buf)
    {
      int len = ::WideCharToMultiByte(CP_UTF8, 0, buf.c_str(), -1, NULL, 0, NULL, NULL);
      if (len == 0) return "";

      string strUtf8;
      strUtf8.resize(len);
      ::WideCharToMultiByte(CP_UTF8, 0, buf.c_str(), -1, (char*)strUtf8.c_str(), len, NULL, NULL);

      return strUtf8;
    }

    string AnsiToUtf8(const string& strSrc)
    {
      return UnicodeToUtf8(AnsiToUnicode(strSrc));
    }

    string AnsiToUtf8(const CString& strSrc)
    {
      return UnicodeToUtf8(AnsiToUnicode(CStringToChar(strSrc)));
    }

  • 相关阅读:
    算法思想篇(5)————贪心算法
    Nginx模块开发(4)————使用subrequest访问第三方服务
    算法思想篇(4)————分治算法
    算法思想篇(3)————递归算法
    算法思想篇(2)————递推算法
    Nginx模块开发(3)————使用upstream访问第三方服务
    CentOS启用iptables防火墙
    linux-TFTP服务
    NFS服务
    linux构建DHCP服务器
  • 原文地址:https://www.cnblogs.com/sancong/p/6406340.html
Copyright © 2011-2022 走看看