Creating Unicode character from its number Creating Unicode character from its number java java

Creating Unicode character from its number


If you want to get a UTF-16 encoded code unit as a char, you can parse the integer and cast to it as others have suggested.

If you want to support all code points, use Character.toChars(int). This will handle cases where code points cannot fit in a single char value.

Doc says:

Converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array. If the specified code point is a BMP (Basic Multilingual Plane or Plane 0) value, the resulting char array has the same value as codePoint. If the specified code point is a supplementary code point, the resulting char array has the corresponding surrogate pair.


Just cast your int to a char. You can convert that to a String using Character.toString():

String s = Character.toString((char)c);

EDIT:

Just remember that the escape sequences in Java source code (the \u bits) are in HEX, so if you're trying to reproduce an escape sequence, you'll need something like int c = 0x2202.


The other answers here either only support unicode up to U+FFFF (the answers dealing with just one instance of char) or don't tell how to get to the actual symbol (the answers stopping at Character.toChars() or using incorrect method after that), so adding my answer here, too.

To support supplementary code points also, this is what needs to be done:

// this character:// http://www.isthisthingon.org/unicode/index.php?page=1F&subpage=4&glyph=1F495// using code points here, not U+n notation// for equivalence with U+n, below would be 0xnnnnint codePoint = 128149;// converting to char[] pairchar[] charPair = Character.toChars(codePoint);// and to String, containing the character we wantString symbol = new String(charPair);// we now have str with the desired character as the first item// confirm that we indeed have character with code point 128149System.out.println("First code point: " + symbol.codePointAt(0));

I also did a quick test as to which conversion methods work and which don't

int codePoint = 128149;char[] charPair = Character.toChars(codePoint);System.out.println(new String(charPair, 0, 2).codePointAt(0)); // 128149, workedSystem.out.println(charPair.toString().codePointAt(0));        // 91, didn't workSystem.out.println(new String(charPair).codePointAt(0));       // 128149, workedSystem.out.println(String.valueOf(codePoint).codePointAt(0));  // 49, didn't workSystem.out.println(new String(new int[] {codePoint}, 0, 1).codePointAt(0));                                                               // 128149, worked

--

Note: as @Axel mentioned in the comments, with java 11 there is Character.toString(int codePoint) which would arguably be best suited for the job.