美文网首页
Unicode16 与 UTF-8编码之间的转换

Unicode16 与 UTF-8编码之间的转换

作者: 阿群1986 | 来源:发表于2019-08-21 15:00 被阅读0次

    Unicode16 与 UTF-8 中文编码之间的转换

    头文件

    #include <string>
    #include <locale>
    #include <codecvt>
    
    std::wstring UnicodeFromUTF8(std::string utf8)
    {
        std::wstring unicode;
        try {
            std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
            unicode = wcv.from_bytes(utf8);
        } catch (const std::exception & e) {
            //std::cerr << e.what() << std::endl;
        }
        return unicode;
    }
    
    std::string UTF8FromUnicode(const std::wstring & unicode)
    {
        std::string utf8;
        try {
            std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
            utf8 = wcv.to_bytes(unicode);
        } catch (const std::exception & e) {
            //std::cerr << e.what() << std::endl;
        }
        return utf8;
    }
    

    ————————————————
    转自CSDN博主「FlushHip」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/FlushHip/article/details/82836867

    相关文章

      网友评论

          本文标题:Unicode16 与 UTF-8编码之间的转换

          本文链接:https://www.haomeiwen.com/subject/edabsctx.html