简体   繁体   English

Java将unicode字符打印到bash shell(Mac OsX)

[英]java print unicode characters to bash shell (mac OsX)

I have this code in java 1.6: 我在Java 1.6中有以下代码:

System.out.println("\u00b2");

but on bash on OSX10.6 I get question marks and not the unicode characters... 但是在OSX10.6的bash上我得到了问号而不是Unicode字符...

actually I want to print the characters 176,177,178 on the extended ascii code (look here http://www.asciitable.com/ ) to create some art on the bash terminal.. 实际上,我想在扩展的ascii代码上打印字符176,177,178(请参见此处http://www.asciitable.com/ ),以在bash终端上创建一些图片。

any idea? 任何想法?

thanks 谢谢

The following code works for me in UTF-8 enabled Terminal.app on Mac OS X 10.6.7: 以下代码在Mac OS X 10.6.7上启用了UTF-8的Terminal.app中为我工作:

# code taken from: 
# "Print Unicode characters to the Terminal with Java",
# http://hints.macworld.com/article.php?story=20050208053951714

echo '
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
class Test {
  public static void main (String[] argv) throws UnsupportedEncodingException {
  String unicodeMessage = "\u00b2\u2591\u2592\u2593";
  PrintStream out = new PrintStream(System.out, true, "UTF-8");
  out.println(unicodeMessage);
  }
}
' > test.java

javac test.java
java Test

Is bash in a UTF8 locale? bash是否在UTF8语言环境中? Type in the locale command and see if your LANG looks appropriate (eg something like *en_GB.UTF-8*). 输入locale命令,看看您的LANG是否合适(例如* en_GB.UTF-8 *之类的东西)。 If not, update the value of LANG and try again. 如果不是,请更新LANG的值,然后重试。

First of all, you need to be certain that the character encoding in your terminal session matches what your java application is outputting. 首先,您需要确定终端会话中的字符编码与Java应用程序输出的字符匹配。 You most likely want UTF-8 which I believe is standard under OS X. 您最可能希望使用UTF-8,我认为它是OS X的标准配置。

Next you need to be certain that the font used in your terminal session actually contains the characters you want to see. 接下来,您需要确定终端会话中使用的字体实际上包含要查看的字符。 These seem to be rare and may not be available in all fonts. 这些似乎很少见,可能并非在所有字体中都可用。

验证是否已选中Terminal > Preferences > Encodings UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM