Java Unicode to hex string [duplicate]
Asked Answered
V

2

8

The code below gives me the Unicode string as கா

sysout = new PrintStream(System.out, true, "UTF-8");
sysout.println("\u0B95\u0bbe");

By giving கா as input, can I get the hex values as \u0B95 and \u0bbe?

PS: This is Tamil language.

Verdi answered 18/5, 2013 at 15:23 Comment(1)
It is not a duplicate I guess. The solution is for single char. But கா is combination of two char. That is why you have two hex values.Verdi
E
6

According to this you'll have to try

System.out.println( "\\u" + Integer.toHexString('க' | 0x10000).substring(1) );

but it will only work on Unicode up to 3.0. If you want to get more values, just create a loop, e.g.

String foo = "கா";
for (int i = 0; i < foo.length(); i++)
    System.out.println( "\\u" + Integer.toHexString(foo.charAt(i) | 0x10000).substring(1));

which produces

\u0b95
\u0bbe

If you want to have them in one line, change System.out.println() to System.out.print() and add System.out.print("\n") in the end.

Engadine answered 18/5, 2013 at 15:28 Comment(3)
Actually i need the hex string of "கா" and not "க". they both are different.Verdi
@Engadine I want the same thing in an opposite way, how can achieve it?Sanjuana
@SantoshJadi try int val = 0x0B95; String str = Character.toString((char) val);Engadine
S
7

You can use the format functionality to print the Java UTF-16 string escapes.

For example, this code writes the escapes to STDOUT:

String str = "கா";
for(char ch : str.toCharArray())
   System.out.format("\\u%04x", (int) ch);
Stocker answered 18/5, 2013 at 18:40 Comment(0)
E
6

According to this you'll have to try

System.out.println( "\\u" + Integer.toHexString('க' | 0x10000).substring(1) );

but it will only work on Unicode up to 3.0. If you want to get more values, just create a loop, e.g.

String foo = "கா";
for (int i = 0; i < foo.length(); i++)
    System.out.println( "\\u" + Integer.toHexString(foo.charAt(i) | 0x10000).substring(1));

which produces

\u0b95
\u0bbe

If you want to have them in one line, change System.out.println() to System.out.print() and add System.out.print("\n") in the end.

Engadine answered 18/5, 2013 at 15:28 Comment(3)
Actually i need the hex string of "கா" and not "க". they both are different.Verdi
@Engadine I want the same thing in an opposite way, how can achieve it?Sanjuana
@SantoshJadi try int val = 0x0B95; String str = Character.toString((char) val);Engadine

© 2022 - 2024 — McMap. All rights reserved.