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

  • 相关阅读:
    Glide优化
    Java多线程知识点
    Android知识点
    Gradle的一些技巧和遇到的问题
    Python用Django写restful api接口
    Python写爬虫爬妹子
    用最简单的例子说明设计模式(三)之责任链、建造者、适配器、代理模式、享元模式
    【Python】扫描指定文件夹下特定后缀的文件
    【Python】生成多级树结构的xml文件
    【转】【Linux】安装pyinstaller
  • 原文地址:https://www.cnblogs.com/sancong/p/6406340.html
Copyright © 2011-2022 走看看