C++11语法转换UTF8很方便,记录一下

inline std::wstring to_wide_string(const std::string& input)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    return converter.from_bytes(input);
}

inline std::string to_byte_string(const std::wstring& input)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    return converter.to_bytes(input);
}

int main()
{
    std::string string = to_byte_string(L"中国飘云阁"); // "\xE4\xB8\xAD\xE5\x9B\xBD\xE9\xA3\x98\xE4\xBA\x91\xE9\x98\x81"
    std::wstring wide_string = to_wide_string("\xE4\xB8\xAD\xE5\x9B\xBD\xE9\xA3\x98\xE4\xBA\x91\xE9\x98\x81"); // 中国飘云阁
}

你可能感兴趣的文章

评论区

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。