【问题标题】:Read Unicode UTF-32 file into wstring将 Unicode UTF-32 文件读入 wstring
【发布时间】:2016-10-25 15:20:10
【问题描述】:

对于 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++ c++11 unicode


【解决方案1】:

您可能希望使用经典的 C++ 模式,即在堆栈而不是堆上分配 wifstream (new):

std::wifstream* file = new std::wifstream(name, ifstream::binary);
std::wifstream file(name, ifstream::binary);

对于 codecvt 部分,我会尝试使用 std::codecvt_utf16&lt;char32_t&gt;

P.S.请注意,wchar_t 在不同平台上可以有不同的大小(16 位、32 位)。因此,对于 UTF-16 和 std::u32string 为 UTF-32 使用 std::u16string 可能会更好。

【讨论】:

  • codecvt std::codecvt_utf16 不起作用。我想我需要像 std::codecvt_utf32 这样的东西,它不存在。我可以创建自己的吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-07-17
  • 2018-10-12
  • 2014-11-14
  • 2010-10-04
  • 2014-09-02
  • 2017-06-20
  • 1970-01-01
相关资源
最近更新 更多