打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
string与wstring转换

方法一:MultiByteToWideChar、WideCharToMultiByte

  1. BOOL StringToWString(const std::string &str,std::wstring &wstr)
  2. {
  3. int nLen = (int)str.length();
  4. wstr.resize(nLen,L' ');
  5. int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
  6. if (nResult == 0)
  7. {
  8. return FALSE;
  9. }
  10. return TRUE;
  11. }
  12. //wstring高字节不为0,返回FALSE
  13. BOOL WStringToString(const std::wstring &wstr,std::string &str)
  14. {
  15. int nLen = (int)wstr.length();
  16. str.resize(nLen,' ');
  17. int nResult = WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wstr.c_str(),nLen,(LPSTR)str.c_str(),nLen,NULL,NULL);
  18. if (nResult == 0)
  19. {
  20. return FALSE;
  21. }
  22. return TRUE;
  23. }

方法二:std::copy

  1. std::wstring StringToWString(const std::string &str)
  2. {
  3. std::wstring wstr(str.length(),L' ');
  4. std::copy(str.begin(), str.end(), wstr.begin());
  5. return wstr;
  6. }
  7. //只拷贝低字节至string中
  8. std::string WStringToString(const std::wstring &wstr)
  9. {
  10. std::string str(wstr.length(), ' ');
  11. std::copy(wstr.begin(), wstr.end(), str.begin());
  12. return str;
  13. }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++宽窄字符转换代码
彻底解密C 宽字符:5、利用fstream转换
char、wchar_t、string、wstring互相转换
CString、LPSTR、std::string、LPCSTR之间的转换
彻底理解C、C++、WIN32与COM中的字符串 - 镜花水月 - JavaEye技术网站
宽字符串和标准字符串的互相转换
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服