java print unicode characters to bash shell (mac OsX)

拥有回忆 提交于 2019-12-24 00:24:29

问题


I have this code in java 1.6:

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

but on bash on OSX10.6 I get question marks and not the unicode characters...

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..

any idea?

thanks


回答1:


The following code works for me in UTF-8 enabled Terminal.app on Mac OS X 10.6.7:

# 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



回答2:


Is bash in a UTF8 locale? Type in the locale command and see if your LANG looks appropriate (e.g. something like *en_GB.UTF-8*). If not, update the value of LANG and try again.




回答3:


First of all, you need to be certain that the character encoding in your terminal session matches what your java application is outputting. You most likely want UTF-8 which I believe is standard under 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.




回答4:


Verify that Terminal > Preferences > Encodings has UTF-8 checked.



来源:https://stackoverflow.com/questions/6384321/java-print-unicode-characters-to-bash-shell-mac-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!