如何使用STL字符串和流读取/存储unicode [英] How to read/store unicode with STL strings and streams

查看:99
本文介绍了如何使用STL字符串和流读取/存储unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改程序以接受Unicode,Unicode可能来自UTF-8以及各种UTF-16和UTF-32编码。我对Unicode的了解并不多(尽管我已经读过Joel Spolsky的 article 维基百科页面)。

I need to modify my program to accept Unicode, which may come from any of UTF-8 and the various UTF-16 and UTF-32 encodings. I don't really know much about Unicode (though I've read Joel Spolsky's article and the Wikipedia page).

右现在我正在使用 std :: istream 并按 char <读取我的输入 char / code>,然后(必要时)将其存储在 std :: string 中。我想

Right now I'm using an std::istream and reading my input char by char, and then storing (when necessary) in an std::string. I'd like to


  • 修改此内容(不费吹灰之力)以支持上述编码,并且

  • 弄清楚如何测试上述编码(我是美国白人面包,并且甚至不知道如何使用另一种编码制作示例文本文件),

  • 以跨平台的方式执行此操作。

此外,如果可能的话,我想尽可能地节省空间可能的(因此,如果我们不需要多于一个字节/字符,则不使用它)。据我了解,这意味着存储在UTF-8中,这很好,但是我不知道执行此操作的标准字符串(据我了解, wchar_t 具有实现定义的大小和编码。)

Also, if possible, I'd like to conserve space as much as possible (so if we don't need more than a byte/character, we don't use it). From what I understand, this means storing in UTF-8, which is fine, but I don't know of a standard string that does this (from what I understand, wchar_t has implementation-defined size and encoding).

推荐答案

看看从嵌入式应用程序从std :: string切换到std :: wstring?问题


如Pukku所说:您可能会因为 C ++标准要求在写入文件时需要宽流才能将双字节字符转换为单字节,并且这种转换的方式取决于实现。

这篇关于如何使用STL字符串和流读取/存储unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆