Surrogate Pair
A surrogate pair is the UTF-16 mechanism that represents one supplementary-plane code point (U+10000 through U+10FFFF) using two 16-bit code units.
Purpose
It lets UTF-16 cover all of Unicode beyond the BMP with 16-bit code units, while keeping 1-unit efficiency for BMP characters.
Mechanism
Subtract 0x10000 from a supplementary code point C to get a 20-bit value in 0-0xFFFFF; the high 10 bits plus 0xD800 give the high surrogate (0xD800-0xDBFF) and the low 10 bits plus 0xDC00 give the low surrogate (0xDC00-0xDFFF). Decoding runs the computation in reverse.
Use cases
- Emoji string handling (e.g., 😀 = U+1F600)
- JavaScript string length and iteration
- Wide-character handling in Windows APIs
- Rendering of ancient scripts and rare characters
Examples
- Input
😀Output\uD83D\uDE00High surrogate D83D plus low surrogate DE00
Common errors
- Splitting a surrogate pair by truncating mid-pair, creating lone surrogates
- Iterate code points with for...of or Array.from
Related terms
UTF-16
UTF-16 is Unicode's variable-length encoding form, using 2 or 4 bytes per code point: BMP characters take one 16-bit code unit and supplementary-plane characters take a surrogate pair (2 code units). GLOSSARY
Unicode
Unicode is the international character encoding standard that assigns a unique code point (U+0000 through U+10FFFF) to every character of nearly all the world's writing systems, with a codespace of 1,114,112 code points. GLOSSARY
Code Point
A code point is the numeric value the Unicode standard assigns to each character (U+0000 to U+10FFFF), the character's position number in the codespace. GLOSSARY
BOM
The BOM (byte order mark) is the Unicode character U+FEFF, placed at the start of a text stream to indicate the encoding form and byte order. GLOSSARY