Converting Unicode Surrogates

Description: These two functions can be used in JavaScript to find the surrogates of a given codepoint and converting those surrogates back to the codepoint.
Tested Platform: All modern browsers
Language: Javascript
// This will get the code point for a high/low pair of surrogates (which are expressed as hexidecimal values)
function getCodePoint(highSurrogate, lowSurrogate) {
    return (highSurrogate - 0xD800) * 0x400 + lowSurrogate - 0xDC00 + 0x10000;

// A function to get the surrogates from a given code point in the astral plain (aka higher than 0xFFFF)
function getSurrogatePair(codePoint) {
    let highSurrogate = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
    let lowSurrogate = (codePoint - 0x10000) % 0x400 + 0xDC00;
    return [highSurrogate, lowSurrogate];

Posted: March 20, 2023

Return to the snippets listing