输出UTF-8(u8)std :: string [英] Output UTF-8 (u8) std::string
问题描述
在C ++ 11和更高版本中,在字符串文字上使用u8
前缀可以创建UTF-8编码的char
(字节)序列.
In C++11 and later, using the u8
prefix on a string literal can create char
(byte) sequences that are UTF-8 encoded.
如何将这些序列输出到std::ostream
?您如何告诉std::ostream
要输出的const char *
或std:string
包含以UTF-8编码的字符,而不是默认编码?
How do you output those sequences to a std::ostream
? How do you tell a std::ostream
that a const char *
or std:string
to be output contains characters encoded in UTF-8, rather than the default encoding?
推荐答案
您没有.流不知道或不在乎文本的编码是什么.尽管它有名称,但std:ostream
不会将char
视为包含以平台编码方式编码的字符.它必须将char
视为要写入的字节.假定您不使用改变其内容的方面,它会按照给定的内容(可能执行\n
转换的方式)写入文本"(字节序列).如果您编写符合UTF-8的字符,那么输出的结果就是这样.
You don't. The stream does not know or care what the encoding of the text is. Despite it's name, a char
is not treated by std:ostream
as containing a character encoded in the platform encoding. It must treats a char
as a byte to be written out. It writes the "text" (byte sequence) as given (outside of possibly performing \n
translation), assuming you don't imbue it with a facet that changes this. If you write characters that conform to UTF-8, then that's what ends up in the output.
这篇关于输出UTF-8(u8)std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!