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

標題: string和wstring相互轉換 [打印本頁]

作者: 51hei不    時間: 2016-6-19 16:55
標題: string和wstring相互轉換
本帖最后由 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. }
復制代碼








歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1