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

  • 相关阅读:
    ADX3000的组网配置
    cuda编程知识普及
    最优的cuda线程配置
    JBOSS在win7环境下启动run.bat无反应
    2013年8月第2个周结
    JBOSS AS 性能调整优化
    jquery插入第一个元素? [问题点数:20分,结帖人zsw19909001]
    @Inject 注入 还是报空指针
    css3 box-shadow阴影(外阴影与外发光)讲解
    事务模板
  • 原文地址:https://www.cnblogs.com/sancong/p/6406340.html
Copyright © 2011-2022 走看看