博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ string 和wstring 之间的互相转换函数
阅读量:7238 次
发布时间:2019-06-29

本文共 1820 字,大约阅读时间需要 6 分钟。

#include 
std::string ws2s(const std::wstring& ws){ std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const wchar_t* _Source = ws.c_str(); size_t _Dsize = 2 * ws.size() + 1; char *_Dest = new char[_Dsize]; memset(_Dest,0,_Dsize); wcstombs(_Dest,_Source,_Dsize); std::string result = _Dest; delete []_Dest; setlocale(LC_ALL, curLocale.c_str()); return result;}std::wstring s2ws(const std::string& s){ setlocale(LC_ALL, "chs"); const char* _Source = s.c_str(); size_t _Dsize = s.size() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _Dsize); mbstowcs(_Dest,_Source,_Dsize); std::wstring result = _Dest; delete []_Dest; setlocale(LC_ALL, "C"); return result;}

 

BOOL StringToWString(const std::string &str,std::wstring &wstr) {         int nLen = (int)str.length();         wstr.resize(nLen,L' ');      int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);      if (nResult == 0)     {         return FALSE;     }      return TRUE; } //wstring高字节不为0,返回FALSE BOOL WStringToString(const std::wstring &wstr,std::string &str) {         int nLen = (int)wstr.length();         str.resize(nLen,' ');      int nResult = WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wstr.c_str(),nLen,(LPSTR)str.c_str(),nLen,NULL,NULL);      if (nResult == 0)     {         return FALSE;     }      return TRUE; }

 

std::wstring StringToWString(const std::string &str) {     std::wstring wstr(str.length(),L' ');     std::copy(str.begin(), str.end(), wstr.begin());     return wstr;  }  //只拷贝低字节至string中 std::string WStringToString(const std::wstring &wstr) {     std::string str(wstr.length(), ' ');     std::copy(wstr.begin(), wstr.end(), str.begin());     return str;  }

 

转载地址:http://fwgfm.baihongyu.com/

你可能感兴趣的文章
如何在Solr中实现多core查询
查看>>
Ubuntu下搭建Hyperledger Fabric v1.0环境
查看>>
EventBus 3.0使用详解
查看>>
Linux curl 一例
查看>>
重写自己,减少判断 ---- 引发的思考
查看>>
【docker】【redis】1.docker安装redis【单点redis服务】
查看>>
Oracle数据库导入导出 imp/exp备份还原
查看>>
react-native-storage + AsyncStorage 实现数据存储
查看>>
Cobaltstrike、armitage联动
查看>>
pandas set_index和reset_index的用法
查看>>
[Bash] View Files and Folders in Bash
查看>>
PEACHPIE 0.9.11 版本发布,可以上生产了
查看>>
异常检测——局部异常因子(Local Outlier Factor ,LOF)算法
查看>>
记录一次广州白云区项目数据库连接失败问题的解决过程
查看>>
干货:Vue粒子特效(vue-particles插件)
查看>>
Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象...
查看>>
加密PDF为只读模式
查看>>
让你编写的控件库在 XAML 中有一个统一的漂亮的命名空间(xmlns)和命名空间前缀...
查看>>
MySQL数据库的锁详解【转】
查看>>
ip route 解释
查看>>