C++ wstring和string相互转换
不支持中文
std::wstring wide = L"wide";
std::string str(wide.begin(), wide.end());
std::string s = "hello";
std::wstring ws(s.begin(), s.end());
支持中文
// Windows
#include <comdef.h>
#include <string>
#include <windows.h>
// -------方式一---------
std::string Wstring2String(std::wstring wstr)
{
// support chinese
std::string res;
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0, nullptr, nullptr);
if (len <= 0){
return res;
}
char* buffer = new char[len + 1];
if (buffer == nullptr){
return res;
}
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, nullptr, nullptr);
buffer[len] = '\0';
res.append(buffer);
delete[] buffer;
return res;
}
std::wstring String2Wstring(std::string wstr)
{
std::wstring res;
int len = MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0);
if( len < 0 ){
return res;
}
wchar_t* buffer = new wchar_t[len + 1];
if( buffer == nullptr){
return res;
}
MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len);
buffer[len] = '\0';
res.append(buffer);
delete[] buffer;
return res;
}
// -------方式二---------
std::string WString2String(const std::wstring& wstr) {
std::string strLocale = setlocale(LC_ALL, ""); // use the default locale set in the environment
const wchar_t* wchSrc = wstr.c_str();
size_t nWStr = wcstombs(NULL, wchSrc, 0) + 1; // get the string len, zh has two byte
char* chDest = new char[nWStr];
memset(chDest, 0, nWStr); // is chDest[nWStr - 1] = 0;
wcstombs(chDest, wchSrc, nWStr); // translates wide characters from the sequence pointed by src to the multibyte equivalent sequence
std::string strRes = chDest;
delete[] chDest;
setlocale(LC_ALL, strLocale.c_str());
bool bUtf8 = IsUtf8((unsigned char*)(strRes.c_str()), strRes.size());
if (bUtf8) {
std::string strGbk;
char c[MAX_PATH_LEN];
strcpy(c, strRes.c_str());
strGbk = Utf8ToGbk(c);
return strGbk;
}
return strRes;
}
std::wstring String2WString(const std::string& str)
{
std::string strGbk = str;
bool bUtf8 = IsUtf8((unsigned char*)(str.c_str()), str.size());
if (bUtf8) {
char c[MAX_PATH_LEN];
strcpy(c, str.c_str());
strGbk = Utf8ToGbk(c);
}
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = strGbk.c_str();
size_t nStr = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nStr];
memset(wchDest, 0, nStr);
mbstowcs(wchDest, chSrc, nStr);
std::wstring wStrRes = wchDest;
delete[] wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wStrRes;
}
// -------方式三---------
std::string WString2String(const std::wstring& wstr){
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(wstr);
}
std::wstring String2WString(const std::string& mbs) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(mbs);
}
// Linux
#include <unistd.h>
#include <sys/stat.h>
#include <iconv.h>
std::string WString2String(const std::wstring& wstr) {
setlocale(LC_ALL, "en_US.UTF-8");
const wchar_t* wchSrc = wstr.c_str();
size_t nWStr = wcstombs(NULL, wchSrc, 0) + 1;
char* chDest = new char[nWStr];
memset(chDest, 0, nWStr);
wcstombs(chDest, wchSrc, nWStr);
std::string strRes = chDest;
delete[] chDest;
return strRes;
}
std::wstring String2WString(const std::string& str)
{
std::string strUtf8 = str;
setlocale(LC_ALL, "en_US.UTF-8");
const char* chSrc = strUtf8.c_str();
size_t nStr = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nStr];
memset(wchDest, 0, nStr);
mbstowcs(wchDest, chSrc, nStr);
std::wstring wStrRes = wchDest;
delete[] wchDest;
return wStrRes;
}
参考链接
https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string
https://www.cnblogs.com/bookdrip/p/9987099.html
https://www.jianshu.com/p/495fb979a1de
https://newbedev.com/how-to-convert-wstring-into-string
https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring/402918#402918
gbk utf8
https://blog.csdn.net/u012234115/article/details/83186386
https://blog.csdn.net/taiyang1987912/article/details/103187978
https://www.jianshu.com/p/a83d398e3606