How to convert one emoji character to Unicode codepoint number in JavaScript?
This is what I use:
const toUni = function (str) {
if (str.length < 4)
return str.codePointAt(0).toString(16);
return str.codePointAt(0).toString(16) + '-' + str.codePointAt(2).toString(16);
};
Added script to convert this on browser side
function emojiUnicode (emoji) {
var comp;
if (emoji.length === 1) {
comp = emoji.charCodeAt(0);
}
comp = (
(emoji.charCodeAt(0) - 0xD800) * 0x400
+ (emoji.charCodeAt(1) - 0xDC00) + 0x10000
);
if (comp < 0) {
comp = emoji.charCodeAt(0);
}
return comp.toString("16");
};
emojiUnicode("ð"); # result "1f600"
thanks to https://www.npmjs.com/package/emoji-unicode
Please Read This Link.
Here is the function :
function toUTF16(codePoint) {
var TEN_BITS = parseInt('1111111111', 2);
function u(codeUnit) {
return '\\u'+codeUnit.toString(16).toUpperCase();
}
if (codePoint <= 0xFFFF) {
return u(codePoint);
}
codePoint -= 0x10000;
// Shift right to get to most significant 10 bits
var leadSurrogate = 0xD800 + (codePoint >> 10);
// Mask to get least significant 10 bits
var tailSurrogate = 0xDC00 + (codePoint & TEN_BITS);
return u(leadSurrogate) + u(tailSurrogate);
}
Two way
let hex = "ð".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);