【发布时间】:2020-07-29 00:08:18
【问题描述】:
当您从std::u16string 转到std::u32string 时,std::wstring_convert 不能像预期的chars 那样工作。那么如何使用std::wstring_convert 以std::u16string 作为输入在UTF-16 和UTF-32 之间进行转换呢?
例如:
inline std::u32string utf16_to_utf32(const std::u16string& s) {
std::wstring_convert<std::codecvt_utf16<char32_t>, char32_t> conv;
return conv.from_bytes(s); // cannot do this, expects 'char'
}
reinterpret_cast 到 char 可以吗,就像我在几个例子中看到的那样?
如果您确实需要reinterpret_cast,我已经看到了一些使用字符串大小而不是指针总字节大小的示例。这是错误还是要求?
我知道codecvt 已被弃用,但在标准提供替代方案之前,它必须这样做。
【问题讨论】:
标签: c++11 utf-16 utf-32 codecvt