- //宽字节转窄字节
- std::string ConvertW2A(const std::wstring &wstr)
- {
- setlocale(LC_ALL, ".936");
- size_t nSize = wstr.length() * 2 + 1;
- char *psz = new char[nSize];
- memset(psz, 0, nSize);
- wcstombs(psz, wstr.c_str(), nSize);
- std::string str = psz;
- delete []psz;
- return str;
- }
- //窄字节转宽字节
- std::wstring ConvertA2W(const string &str)
- {
- setlocale(LC_ALL, ".936");
- size_t nSize = str.length() + 1;
- wchar_t *wpsz = new wchar_t[nSize];
- memset(wpsz, 0, sizeof(wchar_t)*nSize);
- mbstowcs(wpsz, str.c_str(), nSize);
- std::wstring wstr = wpsz;
- delete []wpsz;
- return wstr;
- }
- //该片段来自于http://www.codesnippet.cn/detail/150720134591.html
来源: http://www.codesnippet.cn/detail/150720134591.html