开发者

C++: Printing ASCII Heart and Diamonds With Platform Independent

I'm developing a card playing game and would like to print out the symbol for hearts, diamonds, spades and clubs. My target platform will be Linux.

In Windows, I know how to print out these symbols. For example, to print out a heart (in ASCII) I wrote...

// in Windows, print a ASCII Heart

#include <iostream>

using std::cout;
using std::endl;

int main()
{
 char foo = '\3';
 cout << heart << endl;
 system ( "PAUSE" );
 return 0;
}

However, as I alluded to, a heart symbol won't be printed in Linux. Is there a standard library that can be used to print out a symbol for hearts, diamonds, spades and clubs in both Linux and Windows? What I have been researching so far is looking at Unicode since it is my understanding this is univer开发者_StackOverflow中文版sal.


If you want a portable way, then you should use the Unicode code points (which have defined glyphs associated to them):

♠ U+2660 Black Spade Suit
♡ U+2661 White Heart Suit
♢ U+2662 White Diamond Suit
♣ U+2663 Black Club Suit
♤ U+2664 White Spade Suit
♥ U+2665 Black Heart Suit
♦ U+2666 Black Diamond Suit
♧ U+2667 White Club Suit

Remember that everything below character 32 in ASCII is a control character. They have a meaning associated with them and you don't have a guarantee of getting a glyph or a behavior there (even though most control characters to have glyphs, although they were never intended to be printable). Still, it's not a safe bet.

However, using Unicode needs proper font and encoding support which may or may not be a problem on UNIX-likes.

On Windows at least some of the above code points map to the ASCII control character glyphs you're outputting if the console is set to raster fonts (and therefore not supporting Unicode or anything else than the currently set OEM code page). This only applies to the black variants since the white ones have no equivalent.


On Linux, you can almost always write UTF-8 to stdout and Unicode characters will be displayed beautifully.

#include <iostream>

const char heart[] = "\xe2\x99\xa5";

int main() {
    std::cout << heart << '\n';
    return 0;
}

You can find UTF-8 encodings of Unicode characters on sites like fileformat.info (search that page for "UTF-8 (hex)").

Another way is to use wide characters. You first need to call setlocale to set things up. Then just use wchar_t instead of char and wcout instead of cout.

#include <iostream>
#include <clocale>

const wchar_t heart[] = L"\u2665";

int main() {
    setlocale(LC_ALL, "");
    std::wcout << heart << L'\n';
    return 0;
}


It's not a question of the library, it's a question of the codepage of the font you're using.

  • read about Code page (what you used on Windows, are actually Control Characters)
  • check the ASCII table

If the font used for the terminal you're running on doesn't have hearts and diamonds in it's current code page, no library will help you -- you'd have to use graphics.

For further reading, I'd try to find it maybe in Unicode Tables -- however, unicode terminals are rare... And even if you have a Unicode font, there's no guarantee that it will have the card-images in it.

Bottom line: it all depends on the font, and there's no guaranteed portable way to achieve it without suppling your own gylphs.


The top answer is now out of date or was not correct.

The ASCII codes for clubs, diamonds, hearts, spades are now '\5', '\4', '\3', '\6' respectively.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜