#include <locale>
#include <codecvt>
inline std::string utf8_to_gbk(const std::string& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::wstring tmp_wstr = conv.from_bytes(str);
//GBK locale name in windows
const char* GBK_LOCALE_NAME = ".936";
std::wstring_convert<std::codecvt_byname<wchar_t, char, mbstate_t>> convert(new std::codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
return convert.to_bytes(tmp_wstr);
}
inline std::string gbk_to_utf8(const std::string& str)
{
//GBK locale name in windows
const char* GBK_LOCALE_NAME = ".936";
std::wstring_convert<std::codecvt_byname<wchar_t, char, mbstate_t>> convert(new std::codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
std::wstring tmp_wstr = convert.from_bytes(str);
std::wstring_convert<std::codecvt_utf8<wchar_t>> cv2;
return cv2.to_bytes(tmp_wstr);
}
标签:std,tmp,convert,windows,utf8,gbk,GBK,wstr,wstring
From: https://www.cnblogs.com/nocanstillbb/p/17525196.html