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()));

console.log(xah_codepoint_to_utf8_hexStr(128514) === "F0 9F 98 82");

/*
character: 😂
codepoint 128514
hexadecimal 1f602
utf-8: F0 9F 98 82
utf-16: D8 3D DE 02
*/

Encode a String to UTF8

to encode a string to UTF8, returning array of bytes, do

console.log(
  (new TextEncoder()).encode("😂"),
);

// Uint8Array(4) [ 240, 159, 152, 130 ]

it returns a Uint8Array

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

JavaScript, String, Char, Encoding, Hexadecimal

BUY ΣJS JavaScript in Depth