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

  • 相关阅读:
    动态设置字体大小需要注意的点
    getDimension与getDimensionPixelOffset与getDimensionPixelSize的区别
    统计图钻取的明细报表在非模态窗口中显示
    局部区块多个报表 TAB 页切换及局部区块的参数查询
    分栏报表制作攻略
    多值关联过滤
    鼠标悬停出现提示信息怎么做
    复杂报表设计之动态报表
    Logo(图片)作为报表水印的解决方法
    分组填报表的制作
  • 原文地址:https://www.cnblogs.com/sancong/p/6406340.html
Copyright © 2011-2022 走看看