'How to get the Unicode code point for a character in Javascript?

I'm using a barcode scanner to read a barcode on my website (the website is made in OpenUI5).

The scanner works like a keyboard that types the characters it reads. At the end and the beginning of the typing it uses a special character. These characters are different for every type of scanner.

Some possible characters are:

In my code I use if (oModelScanner.oData.scanning && oEvent.key == "\u2584") to check if the input from the scanner is ▄.

Is there any way to get the code from that character in the \uHHHH style? (with the HHHH being the hexadecimal code for the character)

I tried the charCodeAt but this returns the decimal code.

With the codePointAt examples they make the code I need into a decimal code so I need a reverse of this.



Solution 1:[1]

var hex = "?".charCodeAt(0).toString(16);
var result = "\\u" + "0000".substring(0, 4 - hex.length) + hex;

Solution 2:[2]

If you want to print the multiple code points of a character, e.g., an emoji, you can do this:

const facepalm = "?????";
const codePoints = Array.from(facepalm)
  .map((v) => v.codePointAt(0).toString(16))
  .map((hex) => "\\u{" + hex + "}");
console.log(codePoints);

["\u{1f926}", "\u{1f3fc}", "\u{200d}", "\u{2642}", "\u{fe0f}"]

If you are wondering about the components and the length of ?????, check out this article.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2