0

I'm new to using unicode and I'm currently having trouble in displaying some Chinese text that I received from this translator: http://www.chinese-tools.com/tools/converter-unicode.html

I have successfully utilized unicode in examples when text was only a number like in this case: \u0050 However, the text I'm displaying is more complicated and looks like this: 中国的; when I place "\u" before it I receive a compile-time error that states "Invalid Unicode".

The full line is: System.out.println("\u中国的");

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
Matty-O
  • 21
  • 2
  • 2
    What language/system/context? – deceze Aug 11 '13 at 20:53
  • I am not sure, Is there a way to set it as was done here in html: http://stackoverflow.com/questions/6790861/unicode-characters-in-html – Matty-O Aug 11 '13 at 20:56
  • 1
    You're not sure what language `System.out.println` is from? Java perhaps? – deceze Aug 11 '13 at 21:05
  • Please add a tag indicating what programming language you're using. (Java? C#?) – Keith Thompson Aug 11 '13 at 21:08
  • Oh sorry I misread your question. I am trying to print the following characters in Java: 中国的. I am using the translator http://www.chinese-tools.com/tools/converter-unicode.html to translate these characters and they have brought me to the above print line. – Matty-O Aug 11 '13 at 21:09
  • 1
    You are mixing unrelated notations. 20013 is hex 4E2D. To express the code point 20013 you can use a HTML escape like `中` or equivalently `中` when you are writing HTML, but the way to write that in a string in Java source code is `\u4E2D`. – tripleee Aug 11 '13 at 21:09
  • Sorry Matty, I'm not gonna answer this question since I wrote something very similar and got -1 from somebody, without any explanation. But from what I see, you will get answer :) – Wirus Aug 11 '13 at 21:16
  • I have converted the numbers to hex and changed the print statement to "\u4e2d" + "\u56fd" + "\u7684" However now the return is "???" – Matty-O Aug 11 '13 at 21:17
  • No hard feeling Wirus, I'm sorry you received a -1 for no reason as well. My question received -2 before a single answer, I don't know why people do that. – Matty-O Aug 11 '13 at 21:20
  • I'm guessing people saw your question and a lack of large amounts of code and didn't bother to read it. I don't know the answer to this question, I assume it has something to do with the compiler or your machine not having the fonts installed? The ???'s are probably a result of Java not knowing what those characters look like. – leigero Aug 11 '13 at 21:25
  • This link looks like it would help you. Note the blog mentioned in the second answer: http://stackoverflow.com/questions/18176568/error-in-displaying-unicode – leigero Aug 11 '13 at 21:27
  • It's okay, triplee really helped me. Is there a way to resolve this question without a correct answer to select? – Matty-O Aug 11 '13 at 21:28

1 Answers1

0

I fixed the error by first converting what the translation website returned as a translation into hex as tripleee recommended. Then I created a string.

String foreignWord = ("\u4e2d" + "\u56fd" + "\u7684");
String foreignWordConverted = (new String(foreignWord.getBytes("UTF-8")));
System.out.println( foreignWordConverted );
Matty-O
  • 21
  • 2
  • You shouldn't need to do this... `System.out.println("\u4E2D\u56FD\u7684");` should Just Work. If it doesn't, it sounds like Java has guessed your terminal's encoding incorrectly. Passing the flag `-Dfile.encoding=UTF-8` might fix that without having to work around it in code. – bobince Aug 12 '13 at 10:13
  • [SO](http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding)? :-) It's a standard flag to the `java` command but there are alternative ways to set it like `JAVA_TOOL_OPTIONS` if that's not convenient. Really it's a bug in Java if it has failed to correctly detect your locale, but unfortunately there are a selection of long-standing Java bugs here such that it'll probably never get 100% fixed. – bobince Aug 12 '13 at 22:45
  • @bobince I'll look into this method for further optimization thank you – Matty-O Aug 13 '13 at 15:44