Home Java Character Methods toCodePoint() Java Character toCodePoint() Method Definition In Java, Character.toCodePoint(high, low) converts a surrogate pair into the supplementary code point it stands for. toCodePoint('\uD83D', '\uDE00') returns 128512, which is U+1F600.
The math is the reverse of encoding: take 10 bits from each surrogate, join them into a 20-bit number, and add 0x10000. That is what codePointAt() does internally when it finds a pair.
A static method of java.lang.Character (Java 5+): call it on the class, as in Character.toCodePoint(...); you do not need a Character object. No import is needed, because java.lang is always available.
Syntax Pass the high surrogate first and the low surrogate second.
int a = Character.toCodePoint('\uD83D', '\uDE00'); // 128512 (0x1F600)
int b = Character.toCodePoint('\uD800', '\uDC00'); // 65536 (0x10000)
int c = Character.toCodePoint('\uDBFF', '\uDFFF'); // 1114111 (0x10FFFF) Call What it does Character.toCodePoint(s.charAt(i), s.charAt(i + 1))Decode a pair found at index i. Character.isSurrogatePair(h, l) ? Character.toCodePoint(h, l) : -1Decode only valid pairs. ((h - 0xD800) << 10) + (l - 0xDC00) + 0x10000The same formula by hand.
One overload, toCodePoint(char high, char low), since Java 5.
Library declarations // java.lang.Character (since Java 5)
public static int toCodePoint(char high, char low) Parameters Parameter Description high (char)The high surrogate, 0xD800 to 0xDBFF. low (char)The low surrogate, 0xDC00 to 0xDFFF.
Return Value Return Value Description intThe supplementary code point, 0x10000 to 0x10FFFF, for a valid pair.
Compare with Similar Methods Other ways to get a code point:
Java Examples 1. Decode pairs 2. Check the formula 3. Validate first Example 1 — Decode Three Surrogate Pairs Combine the pairs for the first, an emoji, and the last supplementary code point.
public class CharacterToCodePointValues {
public static void main(String[] args) {
char[][] pairs = { {'\uD800', '\uDC00'}, {'\uD83D', '\uDE00'}, {'\uDBFF', '\uDFFF'} };
for (char[] p : pairs) {
int cp = Character.toCodePoint(p[0], p[1]);
System.out.println(cp + " = 0x" + Integer.toHexString(cp));
}
}
} 65536 = 0x10000
128512 = 0x1f600
1114111 = 0x10ffff How It Works An array of pairs. Each inner char[] holds a high surrogate at index 0 and a low surrogate at index 1.The smallest and largest. 0xD800 with 0xDC00 gives 0x10000, and 0xDBFF with 0xDFFF gives 0x10FFFF.The emoji in the middle. 0xD83D with 0xDE00 gives 0x1F600, the same value codePointAt returns for that string.Example 2 — toCodePoint vs the Decoding Formula Decode the THUMBS UP and G CLEF pairs both ways and print the steps for one of them.
public class CharacterToCodePointFormula {
public static void main(String[] args) {
char[][] pairs = { {'\uD83D', '\uDC4D'}, {'\uD834', '\uDD1E'} };
for (char[] p : pairs) {
int byMethod = Character.toCodePoint(p[0], p[1]);
int byHand = ((p[0] - 0xD800) << 10) + (p[1] - 0xDC00) + 0x10000;
System.out.println("0x" + Integer.toHexString(byMethod) + " same=" + (byMethod == byHand));
}
char h = '\uD83D', l = '\uDC4D';
System.out.println("high bits 0x" + Integer.toHexString(h - 0xD800)
+ ", low bits 0x" + Integer.toHexString(l - 0xDC00));
}
} How It Works Remove the offsets. Subtracting 0xD800 and 0xDC00 leaves two 10-bit numbers: 0x3D and 0x4D for THUMBS UP.Join and shift. 0x3D << 10 is 0xF400; adding 0x4D gives 0xF44D, and adding 0x10000 gives 0x1F44D.Same as the library. Both pairs decode identically by hand and with toCodePoint, including MUSICAL SYMBOL G CLEF (0x1D11E).Example 3 — Decode Safely: isSurrogatePair Before toCodePoint Wrap toCodePoint in a helper that returns -1 for anything that is not a valid pair.
public class CharacterToCodePointSafe {
static int decode(char high, char low) {
return Character.isSurrogatePair(high, low) ? Character.toCodePoint(high, low) : -1;
}
public static void main(String[] args) {
System.out.println("valid pair: " + decode('\uD83D', '\uDE00'));
System.out.println("swapped pair: " + decode('\uDE00', '\uD83D'));
System.out.println("two letters: " + decode('a', 'b'));
}
} valid pair: 128512
swapped pair: -1
two letters: -1 How It Works Check, then convert. The conditional expression only calls toCodePoint when isSurrogatePair has confirmed the order and ranges.A clear failure value. No code point is negative, so -1 cannot be confused with a real result.Why it matters. Without the check, the swapped pair and the two letters would still produce some number, because toCodePoint itself never complains.Frequently Asked Questions What does Character.toCodePoint do? It converts a high surrogate and a low surrogate into the supplementary code point they encode, from 0x10000 to 0x10FFFF.
Does toCodePoint validate its arguments? No. It assumes a valid pair. Call isSurrogatePair(high, low) first if you are not sure.
Which argument comes first? The high surrogate (0xD800 to 0xDBFF), then the low surrogate (0xDC00 to 0xDFFF), the same order as in a String.
What is the formula behind toCodePoint? ((high - 0xD800) << 10) + (low - 0xDC00) + 0x10000.
When should I use codePointAt instead? When the chars are inside a String or array: codePointAt finds the pair, checks it, and decodes it in one call.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion