C++:使用独立于平台的打印 ASCII 心形和钻石
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2094366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Printing ASCII Heart and Diamonds With Platform Independent
提问by different
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.
我正在开发一款纸牌游戏,想打印出红心、钻石、黑桃和梅花的符号。我的目标平台将是 Linux。
In Windows, I know how to print out these symbols. For example, to print out a heart (in ASCII) I wrote...
在 Windows 中,我知道如何打印这些符号。例如,要打印出我写的心形(ASCII)...
// in Windows, print a ASCII Heart
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char foo = '';
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 universal.
但是,正如我所提到的,在 Linux 中不会打印心形符号。是否有一个标准库可用于在 Linux 和 Windows 中打印出心形、菱形、黑桃和俱乐部的符号?到目前为止,我一直在研究的是 Unicode,因为我认为这是通用的。
回答by Joey
If you want a portable way, then you should use the Unicode code points (which have defined glyphs associated to them):
如果您想要一种可移植的方式,那么您应该使用 Unicode 代码点(已定义与其关联的字形):
? 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.
请记住,ASCII 中字符 32 以下的所有内容都是控制字符。它们具有与它们相关的含义,并且您无法保证在那里获得字形或行为(即使大多数控制字符都具有字形,尽管它们从未打算可打印)。尽管如此,这也不是一个安全的赌注。
However, using Unicode needs proper font and encoding support which may or may not be a problem on UNIX-likes.
但是,使用 Unicode 需要适当的字体和编码支持,这在类 UNIX 系统上可能是也可能不是问题。
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.
在 Windows 上,如果控制台设置为光栅字体(因此不支持 Unicode 或除当前设置的 OEM 代码页之外的任何其他内容),至少上述一些代码点映射到您输出的 ASCII 控制字符字形。这仅适用于黑色变体,因为白色变体没有等价物。
回答by Jason Orendorff
On Linux, you can almost always write UTF-8 to stdout and Unicode characters will be displayed beautifully.
在 Linux 上,您几乎总是可以将 UTF-8 写入标准输出,并且 Unicode 字符将被漂亮地显示。
#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)").
您可以在fileformat.info等网站上找到 Unicode 字符的 UTF-8 编码(在该页面搜索“UTF-8(十六进制)”)。
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
.
另一种方法是使用宽字符。您首先需要打电话setlocale
进行设置。然后只需使用wchar_t
代替char
和wcout
代替cout
。
#include <iostream>
#include <clocale>
const wchar_t heart[] = L"\u2665";
int main() {
setlocale(LC_ALL, "");
std::wcout << heart << L'\n';
return 0;
}
回答by Kornel Kisielewicz
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.
为了进一步阅读,我会尝试在Unicode Tables 中找到它——但是,unicode 终端很少见......即使你有一个 Unicode 字体,也不能保证它会有卡片图像。
Bottom line:it all depends on the font, and there's no guaranteed portable way to achieve it without suppling your own gylphs.
底线:这一切都取决于字体,如果不提供您自己的字形,就无法保证可移植的方式来实现它。
回答by Hugh
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.
俱乐部、钻石、红心、黑桃的 ASCII 代码现在'\5', '\4', '\3', '\6'
分别是。