从Java在终端中显示西装符号 [英] Displaying a suit symbol in terminal from Java

查看:103
本文介绍了从Java在终端中显示西装符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,有关在运行Java程序时在终端窗口中显示西装符号(ASCII)(心形,菱形,锹形和棍形)的问题.我目前正在使用JCreator V3 LE.我使用的JDK是1.8.0_172.

I have a question regarding displaying a suit symbol (ASCII) (heart, diamond, spade, and club) to the terminal window when running a Java program. I currently use JCreator V3 LE. The JDK I use is 1.8.0_172.

过去,我使用以下语法:

In the past, I used the syntax:

Character.toString((char) 3)
Character.toString((char) 4)
Character.toString((char) 5)
Character.toString((char) 6)

现在,它显示一个带有?的框.在其中,好像找不到该字符.有另一种方法可以做到这一点,还是从窗口中消除了这个字符?

Now, it displays a box with a ? in it, as if the character cannot be found. Is there another way to do this, or has this character been eliminated from the window?

谢谢.

推荐答案

与所有文本传输一样,您的Java程序(编写器)和终端(读取器)必须位于同一页上,即代码页"(字符编码), 那是.您说的是ASCII,但ASCII不支持您要使用的字符.您可能正在考虑MS-DOS和Windows中的CP437.(MS-DOS没有ASCII代码页;为了完整起见,Windows的寿命很晚.仅在非常特殊的上下文中使用ASCII.)

As with all text transfers, your Java program (writer) and terminal (reader) need to be on the same page—"code page" (character encoding), that is. You said ASCII but ASCII doesn't support the characters you want to use. You are probably thinking of CP437 from MS-DOS and Windows. (MS-DOS didn't have an ASCII code page; Windows got one late in life, for the sake of completeness. ASCII is only used is very specialized contexts.)

如果要将Java的字符转码排除在等式之外,则可以将字节写入输出流.然后,无论它们对终端意味着什么,它都会将其解码为字符.

If you want to take Java's character transcoding out of the equation, you can write bytes to the output stream. Then whatever they mean to the terminal, it will decode them to characters.

// for illustration purposes only; I would not invest in code like this.
System.out.flush();
System.out.write(0x03);
System.out.flush();

要真正看到它们,终端必须具有包含已解码字符的字体.白框或带问号的框表示该字体没有.黑色菱形中的问号或问号表示该字节在终端的字符编码中没有任何意义.

To actually see them, the terminal has to have a font that includes the decoded character. A white box or box with question mark indicate that the font doesn't. A question mark or question mark in a black diamond indicate that the bytes don't mean anything in the terminal's character encoding.

要检查终端的字符编码,请转到 chcp (Windows)或 locale (大多数其他操作系统).

To check your terminal's character encoding, go chcp (Windows) or locale (most other OSes).

正如@VGR在注释中所述,GUI更为简单.这是因为它们避免了使用特定字符编码创建文本字节流的概念,而只是使用窗口系统的功能来绘制文本.(这是以无法将输出通过管道传输到另一个程序或将其重定向到文件为代价的,这是CUI程序的关键功能.)

As @VGR stated in the comments, GUIs are simpler. This is because they avoid the concept of creating a byte stream of text in a particular character encoding and just use the windowing system's facility for drawing text. (This comes at the cost of not being able to pipe the output to another program or redirect it to a file, which is a key feature of CUI programs.)

这篇关于从Java在终端中显示西装符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆