Creating Unicode character from its number

I want to display a Unicode character in Java. If I do this, it works just fine:

String symbol = "\u2202";

symbol is equal to "∂". That's what I want.

The problem is that I know the Unicode number and need to create the Unicode symbol from that. I tried (to me) the obvious thing:

int c = 2202;
String symbol =  "\\u" + c;

However, in this case, symbol is equal to "\u2202". That's not what I want.

How can I construct the symbol if I know its Unicode number (but only at run-time---I can't hard-code it in like the first example)?


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.