Skip to main content
100%

fussing with UTF-16 surrogate encoding in Javascript

✓ Published0🌍 Public
RRandomEtc
Last edited Feb 20, 2012
Created on Feb 20, 2012

This example demonstrates how JavaScript encodes Unicode code points above U+FFFF into UTF-16 surrogate pairs, using functions like `escapeUTF16` and `makeSurrogate` to convert a code point such as U+1F339 (a rose emoji) into the escaped `\ud83c\udf39` sequence. The code implements the surrogate math from the Wikipedia UTF-16 specification, applying bitwise shifts and masking to derive lead and trail surrogates for supplementary planes. It then verifies the result by parsing the escaped string with `JSON.parse` and comparing it to the literal surrogate pair. The source is a standalone gist with no external libraries or data visualization dependencies.

AI-generated description

Similar vizzes