鱼C论坛

 找回密码
 立即注册
查看: 1390|回复: 2

疑问,求助大佬

[复制链接]
发表于 2020-7-21 13:48:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一般用string存汉字没有问题,
但为什么用string将汉字存入vector中最后会变成乱码呢?
萌新求带!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-21 13:53:22 | 显示全部楼层
本帖最后由 xiaosi4081 于 2020-7-21 13:54 编辑

先把它转成utf-8编码试试:
  1. std::string string_To_UTF8(const std::string & str)
  2. {
  3.         int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

  4.         wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
  5.         ZeroMemory(pwBuf, nwLen * 2 + 2);

  6.         ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

  7.         int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

  8.         char * pBuf = new char[nLen + 1];
  9.         ZeroMemory(pBuf, nLen + 1);

  10.         ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

  11.         std::string retStr(pBuf);

  12.         delete[]pwBuf;
  13.         delete[]pBuf;

  14.         pwBuf = NULL;
  15.         pBuf = NULL;

  16.         return retStr;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-21 14:08:48 | 显示全部楼层
用 wstring。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 16:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表