开发者

Special Characters on Console

I've finished my poker game but now I want to make it look a bit better with displaying Spades, Hearts, Diamonds and Clubs. I tried this answer: 开发者_运维技巧C++: Printing ASCII Heart and Diamonds With Platform Independent

Maybe stupid but I try:

cout << 'U+2662' << endl;

I don't know how to write it.


To output a UTF-8 character, you need to encode it as hex bytes. I'll steal this link to fileinfo.com from an answer to the question you linked - if you jump to the UTF-8 representation, it says 0xE2 0x99 0xA5 and you can convert that to "\xE2\x99\xA5" as a string.

However I can't guarantee that your console will display UTF-8 so this answer might not help.


If you are just trying to use the old ASCII control characters in the Windows console, you can use:

cout << (char) 3 << (char) 4 << (char) 5 << (char) 6 << endl;
//hearts, diamonds, clubs, spades

This isn't Unicode, and I assume it is not very portable. I don't really know of a portable solution to your question.


You need to use std::wcout and use UTF-16 by encoding as hex bytes (as Mark answers), but I can't guarantee that any of your characters will display correctly on Windows (before Vista?) because the Windows console was never really intended for this sort of thing. You can use wcout with Visual Studio and maybe Cygwin, but I don't think MinGW supports it. Make sure to use wide character literals: L"string".

Alternatively, you can use the preprocessor to supply the correct definitions of constants for each platform. I imagine that there will be at most three—ASCII on older platforms, UTF-8 on the more modern, and UTF-16 (with wcout) on newer Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜