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) {
     var highSurrogate = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
     var lowSurrogate = (codePoint - 0x10000) % 0x400 + 0xDC00;
     return [highSurrogate, lowSurrogate];

Submitted: January 25, 2013

Return to the snippets listing