JS: Char to UTF-8 Encoding 📜

By Xah Lee. Date: . Last updated: .

Here is a function to convert character to its UTF-8 Encoding.

/*
xah_codepoint_to_utf8_hexStr(cp) return a string of hexadecimal that's the utf8 encoding of the char with codepoint cp (a integer).
The result string, space is inserted between every two hexadecimal digits (byte). Digits are in CAPITAL case.

URL http://xahlee.info/js/js_utf-8_encoding.html
Version: 2022-10-17
*/
const xah_codepoint_to_utf8_hexStr = ((
  xx,
) => ((xx <= 127)
  ? xx.toString(16).toUpperCase()
  : encodeURI(String.fromCodePoint(xx)).replace(
    RegExp("%", "g"),
    " ",
  ).trimStart()));

// s------------------------------

console.log(xah_codepoint_to_utf8_hexStr(129419));
// F0 9F A6 8B

/*
🦋
Name: BUTTERFLY
Codepoint: 129419
HEXD: 1F98B
UTF8: F0 9F A6 8B
UTF16: D83E DD8B
*/

Encode a String to UTF8

/*
to encode a string to UTF8, return array of bytes
*/
console.log((new TextEncoder()).encode("🦋"));
// Uint8Array(4) [ 240, 159, 166, 139 ]

it returns a Uint8Array

Reference: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder