Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Convert String "\uFFFF" into char

Tags:

java

unicode

Is there a standard method to convert a string like "\uFFFF" into character meaning that the string of six character contains a presentation of one unicode character?

like image 958
Dima Avatar asked Jan 24 '10 08:01

Dima


People also ask

What character is \uFFFF?

Unicode uses hexadecimal to represent a character. Unicode is a 16-bit character encoding system. The lowest value is \u0000 and the highest value is \uFFFF.

What is \uFFFF article referred to in java?

\uFFFF is a format of how Unicode is presented in where I read it from (say ASCII file), not a literal.

Can you convert char * to String?

We can convert a char to a string object in java by using the Character. toString() method.


2 Answers

char c = "\uFFFF".toCharArray()[0]; 

The value is directly interpreted as the desired string, and the whole sequence is realized as a single character.

Another way, if you are going to hard-code the value:

char c = '\uFFFF'; 

Note that \uFFFF doesn't seem to be a proper unicode character, but try with \u041f for example.

Read about unicode escapes here

like image 145
Bozho Avatar answered Sep 18 '22 17:09

Bozho


The backslash is escaped here (so you see two of them but the s String is really only 6 characters long). If you're sure that you have exactly "\u" at the beginning of your string, simply skip them and converter the hexadecimal value:

String s = "\\u20ac";  char c = (char) Integer.parseInt( s.substring(2), 16 ); 

After that c shall contain the euro symbol as expected.

like image 25
SyntaxT3rr0r Avatar answered Sep 18 '22 17:09

SyntaxT3rr0r