首页 > 编程语言 >c++ UTF8-GBK互转

c++ UTF8-GBK互转

时间:2023-01-17 09:34:42浏览次数:43  
标签:std tmp convert UTF8 GBK wstring wstr 互转

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

相关文章