找回密码
 立即注册
搜索

wchar_t*转std::string

[复制链接]

wchar_t*转std::string

[复制链接]
admin

108

主题

4

回帖

1万

积分

管理员

积分
18115
2023-10-29 20:11:44 | 显示全部楼层 |阅读模式
  1. std::string wideChar2string(wchar_t* pWCStrKey)
  2. {
  3.         std::wstring wstr(pWCStrKey);
  4.         std::string str(wstr.length(), ' ');
  5.         std::copy(wstr.begin(), wstr.end(), str.begin());
  6.         return str;
  7. }
复制代码


楼主
admin 楼主

108

主题

4

回帖

1万

积分

管理员

积分
18115
2023-10-29 20:38:44 | 显示全部楼层
上面的中文乱码。
  1. std::string wideChar2string(wchar_t* unicode)
  2. {
  3.         int len;
  4.         len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
  5.         char* szUtf8 = (char*)malloc(len + 1);
  6.         memset(szUtf8, 0, len + 1);
  7.         WideCharToMultiByte(CP_OEMCP, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
  8.         //WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
  9.         return szUtf8;
  10.         std::string str1(szUtf8);
  11.         return str1;
  12. }
复制代码

QQ|Archiver|小黑屋|mtctp |

GMT+8, 2024-5-9 02:56 , Processed in 0.049499 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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