Special Characters on Console

元气小坏坏 提交于 2019-12-13 13:43:20

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2623881/special-characters-on-console

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