C++中,宽窄字符互换(wchar_t char wstring string互换)
宽字符转窄字符(wstring转string):
std::string WstringToString(const std::wstring& sInput) {
if (sInput.length() == 0)
return "";
int iBufLen = WideCharToMultiByte(CODE_PAGE, 0, sInput.c_str(), -1, NULL, 0, NULL, NULL);
char* buf = new char[iBufLen + 1];
int iLen = WideCharToMultiByte(CODE_PAGE, 0, sInput.c_str(), -1, buf, iBufLen + 1, NULL, NULL);
std::string sOutput(buf, iLen);
delete[] buf;
return sOutput;
}
窄字符转宽字符(string转wstring):
std::wstring utils::StringToWstring(const std::string& sInput) {
if (sInput.length() == 0)
return L"";
int iBufLen = MultiByteToWideChar(CODE_PAGE, 0, sInput.c_str(), -1, NULL, 0);
wchar_t* buf = new wchar_t[iBufLen + 1];
int iLen = MultiByteToWideChar(CODE_PAGE, 0, sInput.c_str(), -1, buf, iBufLen + 1);
std::wstring sOutput(buf, iLen);
delete[] buf;
return sOutput;
}
其中,涉及到wchar_t,char_t的转换,可由上面的函数修改一二可得。

浙公网安备 33010602011771号