Output UTF-8 (u8) std::string

老子叫甜甜 提交于 2020-01-06 04:29:05

问题


In C++11 and later, using the u8 prefix on a string literal can create char (byte) sequences that are UTF-8 encoded.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/47705733/output-utf-8-u8-stdstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!