由于C++17之后取消std::codecvt_utf8的支持,所以UTF8与UNICODE之间进行转换,就需要使用别的方法了。否则代码是编译不过,这时又需要回到WINDOWS底层的API来调用转换了。需要使用MultiByteToWideChar函数来实现,下面就是实现UTF8到UNICODE之间转换:
const std::wstring CCaiStep::Utf8ToWString(const std::string utf8)
{
//std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; //转换UTF8保存
//std::wstring wstrTemp = conv.from_bytes(str);
//return wstrTemp;
std::wstring utf16; // Result
if (utf8.empty())
{
return utf16;
}
constexpr DWORD kFlags = MB_ERR_INVALID_CHARS;
if (utf8.length() > static_cast<size_t>( (std::numeric_limits<double>::max)() ))
{
return utf16;
}
const int utf8Length = static_cast<int>(utf8.length());
const int utf16Length = ::MultiByteToWideChar(
CP_UTF8, // Source string is in UTF-8
kFlags, // Conversion flags
utf8.data(), // Source UTF-8 string pointer
utf8Length, // Length of the source UTF-8
|