//wstring转string
int convert_wstring_to_string(const wstring in_wstr, string& out_str)
{
if (in_wstr.empty()) return 1;
const wchar_t* wstr = in_wstr.c_str();
char* str = NULL;
int size = 0;
string loc = setlocale(LC_ALL, NULL);//获得默认语言环境
setlocale(LC_ALL, "chs"); //设置语言环境为chs
size = wcstombs(NULL, wstr, 0);//获得转换后的宽字符数,不含终止符
str = new char[size + 1];
wcstombs(str, wstr, size); //转换为char字符存于数组str
str[size] = ‘\0‘;
out_str = str;
delete[] str;
setlocale(LC_ALL, loc.c_str()); //恢复默认语言环境
return 0;
}
// string 转 wstring
int convert_string_to_wstring(const string in_str, wstring& out_wstr)
{
if (in_str.empty()) return 1;
const char* str = in_str.c_str();
wchar_t* wcs = NULL;
int size = 0;
string loc = setlocale(LC_ALL, NULL);//获得默认语言环境
setlocale(LC_ALL, "chs"); //设置语言环境为chs
size = mbstowcs(NULL, str, 0);
wcs = new wchar_t[size + 1];
size = mbstowcs(wcs, str, size + 1);//转换为宽字符存于数组wcs
wcs[size] = 0;
out_wstr = wcs;
delete[] wcs;
setlocale(LC_ALL, loc.c_str());
return 0;
}
unicode编码对照表
参考链接:https://blog.csdn.net/zhouguangcai8/article/details/80264184
原文:https://www.cnblogs.com/BadgerLee/p/14073765.html