c++ - 将 Unicode UTF-32 文件读入 wstring

标签 c++ c++11 unicode

对于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/

相关文章:

.net - Windows窗体控件库;托管和非托管

c++ - 通过更改类型在函数之间传递模板对象

c++ - 在读取数据文件时,如何找到用户输入的字符串的变位词?

html - 右下箭头是否有 html 特殊字符?

java - 如何将 "plaintext"Java 源代码点以编程方式转换为 Python3 中的表情符号?

c++ - 二叉树深度

c++ - 为什么在C++宏上扩展为注释时会出现 “Expected a declaration”错误?

c++ - 安全使用 C++ STL 的规则/指南

c++ - 标准 C++ 库文件夹中的元组文件已损坏。如何修改?

sql-server - LIKE 忽略魔法 Unicode 字符