简体   繁体   中英

How to print wide character string?

The ISO-10646 code point value of Cyrillic lowercase a is 0x430, so I tried the following:

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 .

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 ). 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. 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 . 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. 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?

From: 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.

So only use one of the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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