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,UTF8,GBK,wstring,wstr,互转 From: https://www.cnblogs.com/m1911/p/17056978.html