HOWTO · Java
How to Get Unicode Characters from its Number in Java
This article introduces how to get Unicode characters from its number in Java.
On this page
A Java char stores one 16-bit UTF-16 code unit, while a Unicode code point can require one or two char values. For a code point held as an int, use Character.toChars() and wrap the resulting array with String.valueOf(). This works for BMP and supplementary code points. A cast to char is only suitable when the value fits in one code unit; casting a supplementary value discards its upper bits.
Understand Unicode Code Points and Java char Values
A code point is a number assigned to a Unicode character or reserved position. Java strings store UTF-16 code units, so the number of char values in a string is not always the number of code points it contains. A BMP code point is encoded in one char; a supplementary code point is encoded as a high and low surrogate pair. Treating either surrogate separately does not produce the original character.
Java’s code point range is 0 through 0x10FFFF. Unicode scalar values exclude the surrogate range 0xD800 through 0xDFFF, so applications that accept arbitrary user-supplied numbers may need to reject those values as well. Character.isValidCodePoint(int) checks the numeric range; it does not check whether a value is a Unicode scalar value.
Convert a BMP Code Point to a String
For a BMP value that represents a character, casting to char produces its single UTF-16 code unit. String.valueOf(char) then creates a one-unit string. For example, code point 0x2202 (the partial-derivative symbol) can be converted with String.valueOf((char) 0x2202). Keep this shortcut limited to BMP values: Java’s cast narrows the integer and does not validate it, so supplementary values can be changed silently.
Convert Any Code Point to a String
Use Character.toChars(int) for general conversion. It returns a one-element array for a BMP value and a two-element surrogate-pair array for a supplementary value. String.valueOf(char[]) turns either result into a string without losing the pair. For example, Character.toChars(0x1F642) represents the slightly smiling face as two UTF-16 code units even though it is one code point. The resulting string’s length() is therefore two, while codePointCount(0, string.length()) is one.
Run this example:
public class UnicodeCodePoint {
public static void main(String[] args) {
int codePoint = 0x1F642;
String value = String.valueOf(Character.toChars(codePoint));
System.out.println(value);
}
}
🙂
Before converting external integer input, call Character.isValidCodePoint(codePoint) and handle values outside the supported range. Character.toChars() also rejects an out-of-range value with IllegalArgumentException; the cast approach does not provide that protection.
Parse a Hexadecimal Code Point
When input arrives as hexadecimal text, parse it in radix 16 and pass the resulting integer to the general conversion. For example, Integer.parseInt("1F642", 16) parses the digits without a 0x prefix, and String.valueOf(Character.toChars(parsed)) creates the string. Integer.parseInt reports malformed text or a value too large for an int with NumberFormatException; after parsing, still check the Unicode range because many valid integers are not code points. A parsed value in the surrogate range is numerically within Java’s code point range, but it is not a Unicode scalar value. Handle parse and range failures where the input is untrusted.