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 /* character: 😂 codepoint 128514 hexadecimal 1f602 utf-8: F0 9F 98 82 utf-16: D8 3D DE 02 */ console.log(xah_codepoint_to_utf16_hexStr(128514) === "D83D DE02");