欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2156|回復: 0
打印 上一主題 下一主題
收起左側

string和wstring相互轉換

[復制鏈接]
跳轉到指定樓層
樓主
ID:127229 發表于 2016-6-19 16:55 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 51hei不 于 2016-6-19 16:57 編輯

第一種方法:調用WideCharToMultiByte()和MultiByteToWideChar(),代碼如下(關于詳細的解釋,可以參考《windows核心編程》):
  1. #include <string>
  2. #include <windows.h>
  3. using namespace std;
  4. //Converting a WChar string to a Ansi string
  5. std::string WChar2Ansi(LPCWSTR pwszSrc)
  6. {
  7. int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
  8. if (nLen<= 0) return std::string("");
  9. char* pszDst = new char[nLen];
  10. if (NULL == pszDst) return std::string("");
  11. WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
  12. pszDst[nLen -1] = 0;
  13. std::string strTemp(pszDst);
  14. delete [] pszDst;
  15. return strTemp;
  16. }
  17. string ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); }
  18. //Converting a Ansi string to WChar string
  19. std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
  20. {
  21. int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
  22. if(nSize <= 0) return NULL;
  23. WCHAR *pwszDst = new WCHAR[nSize+1];
  24. if( NULL == pwszDst) return NULL;
  25. MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
  26. pwszDst[nSize] = 0;
  27. if( pwszDst[0] == 0xFEFF) // skip Oxfeff
  28. for(int i = 0; i < nSize; i ++)
  29. pwszDst[i] = pwszDst[i+1];
  30. wstring wcharString(pwszDst);
  31. delete pwszDst;
  32. return wcharString;
  33. }
  34. std::wstring s2ws(const string& s){ return Ansi2WChar(s.c_str(),s.size());}
復制代碼



第二種方法:采用ATL封裝_bstr_t的過渡:(注,_bstr_是Microsoft Specific的,所以下面代碼可以在VS2005通過,無移植性);
  1. #include <string>
  2. #include <comutil.h>
  3. using namespace std;
  4. #pragma comment(lib, "comsuppw.lib")
  5. string ws2s(const wstring& ws);
  6. wstring s2ws(const string& s);
  7. string ws2s(const wstring& ws)
  8. {
  9. _bstr_t t = ws.c_str();
  10. char* pchar = (char*)t;
  11. string result = pchar;
  12. return result;
  13. }
  14. wstring s2ws(const string& s)
  15. {
  16. _bstr_t t = s.c_str();
  17. wchar_t* pwchar = (wchar_t*)t;
  18. wstring result = pwchar;
  19. return result;
  20. }
復制代碼



第三種方法:使用CRT庫的mbstowcs()函數和wcstombs()函數,平臺無關,需設定locale。
  1. #include <string>
  2. #include <locale.h>
  3. using namespace std;
  4. string ws2s(const wstring& ws)
  5. {
  6. string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  7. setlocale(LC_ALL, "chs");
  8. const wchar_t* _Source = ws.c_str();
  9. size_t _Dsize = 2 * ws.size() + 1;
  10. char *_Dest = new char[_Dsize];
  11. memset(_Dest,0,_Dsize);
  12. wcstombs(_Dest,_Source,_Dsize);
  13. string result = _Dest;
  14. delete []_Dest;
  15. setlocale(LC_ALL, curLocale.c_str());
  16. return result;
  17. }
  18. std::wstring s2ws(const std::string& s)
  19. {
  20.       std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  21.       setlocale(LC_ALL, "chs");
  22.       const char* _Source = s.c_str();
  23.       size_t _Dsize = s.size() + 1;
  24.       wchar_t *_Dest = new wchar_t[_Dsize];
  25.       wmemset(_Dest, 0, _Dsize);
  26.       mbstowcs(_Dest,_Source,_Dsize);
  27.       std::wstring result = _Dest;
  28.       delete []_Dest;
  29.       setlocale(LC_ALL, curLocale.c_str());
  30.       return result;
  31. }
復制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表