Converting Hexadecimal to UTF-16 FormatLet's imagine you're trying to insert the 🖤 symbol.
The characters in this range are described as being in the "Basic Multilingual Plane" (BMP). Characters with a code place above 0x10000 are best described as "non-BMP characters." And, we want one of those!
How To Covert to the UTF-16 FormatThe first thing to know is that the first four-character hexadecimal number is called the "high surrogate." The second one of the pairing is called the "low surrogate." Together, they are called a "surrogate pair," and they represent one character.
let codePlace = 0x1F49C;|
let highSurrogate = Math.floor((codePlace - 0x10000) / 0x400) + 0xD800;
let lowSurrogate = (codePlace - 0x10000) % 0x400 + 0xDC00;
|Get Hexadecimal String|
let stringOne = highSurrogate.toString(16);|
let stringTwo = lowSurrogate.toString(16);
|Creating the Surrogate Pair|
|let surrogatePair = "\\u"+ stringOne + "\\u" + stringTwo;|
let str = "\uD83D\uDDA4"
document.write("My symbol: " + str)
|My symbol: 🖤|
UTF-16 CalculatorUse this calculator to get the UTF-16 format for your character.
For CompletenessAnd, just for completeness, if you need to convert from a hexadecimal string to a number:
|Get Numbers from the Hexadecimal String (if needed)|
myFirstNumber = parseInt(stringOne, 16);|
mySecondNumber = parseInt(stringTwo, 16);
Help Us Improve Cyber Definitions
- Do you disagree with something on this page?
- Did you spot a typo?
- Do you know a slang term that we've missed?