Chetan Chetan - 5 months ago 19
Java Question

Get Unicode Encoded Characters from given String

When I print

s1
, I get the result as
Hello
.
When I print
s2
, I get the result as
???????
.

String s1="\u0048\u0065\u006C\u006C\u006F"; // Hello
String s2="\u0CAE\u0CC1\u0C96\u0CAA\u0CC1\u0C9F"; // ಮುಖಪುಟ (Kannada Language)

System.out.println("s1: " + StringEscapeUtils.unescapeJava(s1)); // s1: Hello
System.out.println("s2: " + StringEscapeUtils.unescapeJava(s2)); // s2: ???????


I want the output
ಮುಖಪುಟ
for
s2
. How can I achieve this?

Answer
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 PrintStream ps = new PrintStream(os);
 ps.println("\u0048\u0065\u006C\u006C\u006F \u0CAE\u0CC1\u0C96\u0CAA\u0CC1\u0C9F");  
 String output = os.toString("UTF8");
 System.out.println("result: "+output);   //  Hello ಮುಖಪುಟ 
Comments