对于UTF-16,我们可以同时读取并转换为wchar
。例如,
std::wifstream* file = new std::wifstream(name, ifstream::binary);
locale lo = locale(file->getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>);
file->imbue(lo);
如何对 UTF-32 输入执行相同的操作?
最佳答案
您可能希望使用经典的 C++ 分配模式 wifstream
在堆栈而不是堆上(new
):
std::wifstream* file = new std::wifstream(name, ifstream::binary);
std::wifstream file(name, ifstream::binary);
对于 codecvt 部分,我会尝试使用 std::codecvt_utf16<char32_t>
.
P.S. 注意 wchar_t
在不同平台上可以有不同大小(16 位、32 位)。所以你最好使用std::u16string
对于 UTF-16 和 std::u32string
对于 UTF-32。
关于c++ - 将 Unicode UTF-32 文件读入 wstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40243760/