Dzwebs.Net

撰写电脑技术杂文十余年

c++c++ string转中文utf-8的函数代码

Admin | 2022-3-30 8:34:52 | 被阅次数 | 6878

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  一、函数代码如下

  std::string string_To_UTF8(const std::string& str)

  {
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
 wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 
 ZeroMemory(pwBuf, nwLen * 2 + 2);
 ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
 int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
 char* pBuf = new char[nLen + 1];
 ZeroMemory(pBuf, nLen + 1);
 ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
 std::string retStr(pBuf);
 delete[]pwBuf;
 delete[]pBuf;
 pwBuf = NULL;
 pBuf = NULL;
 return retStr;
  }

  二、使用方法

string result = string_To_UTF8("大众计算机学习网");


该杂文来自: 基础知识杂文

上一篇:冰点还原精灵激活为永久有效不过期的方法

下一篇:c++分割图片的base64编码

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计