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
网友评论