std字符串转换函数

#include <ShlObj.h> #include <Psapi.h> std::string SW2A(std::wstring& strW) { std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const wchar_t* _Source = strW.c_str(); size_t _Dsize = 2 * strW.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::string SW2A(const wchar_t* szW) { std::wstring strW = L""; if (szW != NULL) { strW = szW; } return SW2A(strW); } std::wstring SA2W(std::string& strA) { setlocale(LC_ALL, "chs"); const char* _Source = strA.c_str(); size_t _Dsize = strA.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; } std::wstring SA2W(const char* szA) { std::string strA = ""; if (szA != NULL) { strA = szA; } return SA2W(strA); }

你可能感兴趣的:(std字符串转换函数)