javascriptemojiemojione

How to convert one emoji character to Unicode codepoint number in JavaScript?


how to convert this 😀 into this 1f600 in javascript

'😀'.charCodeAt(0);  

this will return unicode 55357 but how to get 1f600 from 😀


Solution

  • 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