JS: Char to UTF-16 Encoding 📜
Here is a function that return the hexadecimal string of UTF-16 Encoding of a unicode character's codepoint.
/* xah_codepoint_to_utf16_hexStr(zcodepoint) return a string of hexadecimal that's the UTF-16 encoding of the char of codepoint zcodepoint (integer). The result string is not padded, but one space is inserted to separate the first 2 bytes and last 2 bytes. Digits are in CAPITAL case. URL http://xahlee.info/js/js_utf-16_encoding.html Version: 2022-10-17 2025-12-22 */ const xah_codepoint_to_utf16_hexStr = (zcodepoint) => { const xCharStr = String.fromCodePoint(zcodepoint); if (zcodepoint < 2 ** 16) { return xCharStr.charCodeAt(0).toString(16).toUpperCase(); } else { const xLen = String.fromCodePoint(zcodepoint).length; const xout = []; for (let i = 0; i < xLen; i++) { xout.push(xCharStr.charCodeAt(i).toString(16).toUpperCase()); } return xout.join(" "); } }; // s------------------------------ // test console.assert(xah_codepoint_to_utf16_hexStr(129419) === "D83E DD8B"); /* 🦋 BUTTERFLY ID 129419 HEXD 1F98B UTF8 F0 9F A6 8B UTF16 D83E DD8B */
JavaScript. String, Char, Encoding, Hexadecimal
- JS: String Code Unit
- JS: Convert Decimal, Hexadecimal
- JS: String.prototype.codePointAt (Char to Char ID) ❌
- JS: String.fromCodePoint (Char ID to Char)
- JS: Char to UTF-8 Encoding 📜
- JS: Char to UTF-16 Encoding 📜
- JS: String.prototype.charCodeAt (Char to Char ID) ❌
- JS: String.prototype.charAt (Extract Char at Index) ❌
- JS: String.prototype.at (Extract Char at Index)
- JS: String.fromCharCode (Char ID to Char) ❌