Sign in to follow this  
Lode

std::cout special ASCII symbols

Recommended Posts

Hello, I'm making a simple card game that, for now as prototype, works in the terminal. I use special card characters (these have very low ascii values, like 003-006, I remembered these from my QBasic days). My question is simply, are these characters normally well supported in the terminal? I tried in Linux, and it works there. But I'm wondering if it also works in all flavors of Windows, and if this is considered to be portable in general? So, what does the following code print for you? std::cout << "A♥ T♦ 3♣ 4♠" << std::endl;

Share this post


Link to post
Share on other sites
Win Vista, MSVC 2008: A? T? 3? 4?

However, it is possible to get the console to display those symbols (edit: provided that the user's console font contains them). One example:

WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), L"A♥ T♦ 3♣ 4♠", 11, 0, 0);

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Win Vista, MSVC 2008: A? T? 3? 4?

However, it is possible to get the console to display those symbols (edit: provided that the user's console font contains them). One example:

WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), L"A♥ T♦ 3♣ 4♠", 11, 0, 0);


Ok, that will require some #ifdefs to work on both Windows and Linux in that case.

About the console font: will the user's console font normally contain these characters? Is it so when the user didn't modify any settings?

Share this post


Link to post
Share on other sites
For these particular four characters, I believe the default font will contain them, but I'm not one hundred percent sure this will be true for all versions of Windows.

Share this post


Link to post
Share on other sites
Are there any guarentees that the 0x00-0x1F ASCII codes produce specific symbols?

As far as I know the ASCII standards define everything below 0x20 as various control codes and doesn't say anything about specific symbols.

Share this post


Link to post
Share on other sites
Doesn't seem to work for me (Windows 7).

The console font seems to contain those characters (can paste them in fine), but std::cout from a program just prints this "A? T? 3? 4?"... so not sure what's going on there.

Share this post


Link to post
Share on other sites
I remember i did that with some hexadecimal values without need of any platform specific functions:

\u + icon_code

i/c name
2660 Black Spade
2661 White Heart
2662 White Diamond
2663 Black Club
2664 White Spade
2665 Black Heart
2666 Black Diamond
2667 White Club

Example:
std::cout << "\u2660"; //black spade
std::cout << "\u2663"; //black club

etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kasya

Example:
std::cout << "\u2660"; //black spade
std::cout << "\u2663"; //black club

etc.


That gives indeed those symbols in the Konsole terminal in Linux if I try.

Could anyone else try with his/her Windows flavor? Thanks! :)

Share this post


Link to post
Share on other sites
In the windows 7 console I can type those characters easily just by typing them in (eg: alt-6), or typing them as input to C#'s Console.Write('☺') command.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
Quote:
Original post by Kasya

Example:
std::cout << "\u2660"; //black spade
std::cout << "\u2663"; //black club

etc.


That gives indeed those symbols in the Konsole terminal in Linux if I try.

Could anyone else try with his/her Windows flavor? Thanks! :)


Nope, still ?'s for me... (Win 7).

EDIT: This seems to work though:


int main(int, char*[]) {
char heart = '\3';
char diamond = '\4';
char club = '\5';
char spade = '\6';

std::string all = "\3\4\5\6";

std::cout << heart << diamond << club << spade << std::endl;
std::cout << all << std::endl;
}


[Edited by - sprite_hound on April 23, 2010 2:26:37 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by sprite_hound
Quote:
Original post by Lode
Quote:
Original post by Kasya

Example:
std::cout << "\u2660"; //black spade
std::cout << "\u2663"; //black club

etc.


That gives indeed those symbols in the Konsole terminal in Linux if I try.

Could anyone else try with his/her Windows flavor? Thanks! :)


Nope, still ?'s for me... (Win 7).

EDIT: This seems to work though:


int main(int, char*[]) {
char heart = '\3';
char diamond = '\4';
char club = '\5';
char spade = '\6';

std::string all = "\3\4\5\6";

std::cout << heart << diamond << club << spade << std::endl;
std::cout << all << std::endl;
}


What could be an explanation for that? This is strange... Why would std::cout print characters in different ways in the terminal depending on how you give it as string?

Share this post


Link to post
Share on other sites
Windows terminals use the Terminal font, which only has support for the IBM character set. When you paste "A♥ T♦ 3♣ 4♠" into your IDE, you're using the Unicode counterparts, which, under the Terminal font that Windows uses, shows up as '?'. I suspect that a combination of changing the Windows terminal font to a non-bitmap font (such as Arial 'n' such) or compiling with _UNICODE or using wcout and wchar_t literals will fix the problem.

Share this post


Link to post
Share on other sites
Aww dammit, the '\3' variant shows up wrong in Linux, it becomes a small box with "0003" in it.

Anyway, easily solved by OS-dependent #ifdefs :)

Thanks for the info.

Share this post


Link to post
Share on other sites
The console subsystem in Windows is a magical and special place. If you're dead set on doing a console application, the best bet is to follow the advice in this and this blog post.

As for narrow output streams like std::cout, they pass through whatever bytes you pass them, so if you feed it UTF-8 encoded text, they will output UTF-8 encoded text. In the case of Linux, if the terminal locale is set to UTF-8, it'll display properly.

In the case of the Windows console, the data displayed will be shown in the current system codepage, unless you've selected another codepage into it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this