简体   繁体   English

如何打印宽字符串?

[英]How to print wide character string?

The ISO-10646 code point value of Cyrillic lowercase a is 0x430, so I tried the following: 西里尔字母小写a的ISO-10646代码点值是0x430,因此我尝试了以下操作:

char u8str[] = u8"Cyrillic lowercase a is: \u0430.";
cout << u8str;

and

wchar_t wstr[] = L"Cyrillic lowercase a is: \u0430.";
wcout << wstr;

The Cyrillic lowercase a is successfully printed through u8str , but not wstr . 西里尔小写一个是通过成功地打印u8str ,但不是wstr

As for u8str , I've confirmed that its storage is initialized with the utf-8 encoding values of those characters (the Cyrillic lower case a occupies 2 bytes with the value D0 B0 ). 至于u8str ,我已经确认它的存储已使用这些字符的utf-8编码值进行了初始化(西里尔字母小写a占2个字节,值D0 B0 )。 All seems alright. 一切似乎还好。 The Cyrillic got printed correctly. 西里尔字母正确打印。

As for wstr , I suppose that each wchar_t in the wstr array is initialized with the numerical value of the encoding of the character in the execution wide-character set. 至于wstr ,我假设wstr数组中的每个wchar_t都用执行宽字符集中的字符编码的数值初始化。 While I don't fully understand what execution wide-character set is, I checked that the value of the Cyrillic lowercase a stored in the array is 0x430 . 尽管我不完全了解execution wide-character set是什么,但我检查了数组中存储的西里尔小写字母a的值为0x430 Anyway, the Cyrillic letter doesn't get printed correctly. 无论如何,西里尔字母不能正确打印。 (Other characters are all OK.) (其他字符都可以。)

I'm a total novice for wchar_t stuffs, so my apology if this question is too elementary. 我是wchar_t新手,如果这个问题太基础,我向您道歉。 What went wrong in my attempt printing the Cyrillic letter using wide character string? 尝试使用宽字符串打印西里尔字母时出了什么问题? Is it an issue of the letter's representation in the execution wide-character set (what is this character set after all)? 这是执行宽字符集中的字母表示形式的问题吗(此字符集到底是什么)? Or is it an issue about the incorrect usage of iostream facilities? 还是有关iostream工具使用不正确的问题?

From: http://www.cplusplus.com/reference/iostream/cout/ 来自: http : //www.cplusplus.com/reference/iostream/cout/

A program should not mix output operations on cout with output operations on wcout (or with other wide-oriented output operations on stdout): Once an output operation has been performed on either, the standard output stream acquires an orientation (either narrow or wide) that can only be safely changed by calling freopen on stdout. 程序不应将cout上的输出操作与wcout上的输出操作(或stdout上的其他宽方向的输出操作)混合:一旦在任一方向上执行了输出操作,则标准输出流将获得方向(窄或宽)只能通过在stdout上调用freopen来安全地更改它。

So only use one of the two. 因此,只能使用两者之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM