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

8

26

how to convert this πŸ˜€ into this 1f600 in javascript

'πŸ˜€'.charCodeAt(0);  

this will return unicode 55357 but how to get 1f600 from πŸ˜€

Generality answered 24/1, 2018 at 9:41 Comment(7)
This npm package has method to convert into Unicode. npmjs.com/package/emoji-unicode – Linesman
@gurvinder372 i already tried your solution it is not working. This is not duplicate question – Generality
@MitulGedeeya how to do it in browser ?? – Generality
@ParthGajjar Share what you tried in the question itself so that we know what you have already tried. Create a working snippet demonstrating the issue itself. – Peterpeterborough
Sorry it will be server side rendering. :) let me give you answer with the function – Linesman
@MitulGedeeya used same code and converted for browser side Thanks :) – Generality
okay and you also can try my answer below. – Linesman
G
19

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

Generality answered 24/1, 2018 at 10:11 Comment(8)
That's wrong. When you removed the returns from the original function and didn't add the proper ifs and elses you altered the function. Correct would be how it is here: raw.githubusercontent.com/IonicaBizau/emoji-unicode/… – Gloriane
What about emojis that are a combination of two others and have charCodeAt from 0 to 4? E.g.: πŸ€¦β€β™€οΈ – Agra
(Javascript), In case you need to convert it back to Emoji again use: String.fromCodePoint(parseInt ("1f600", 16)) – Zonked
Country flag doesn't render properly – Womanlike
@Parth Gajjar: sir can you tell me how to get get emoji from \uD83D\uDE0A this string? – Lindsley
Simple Heart 'β™₯' returns NaN – Muirhead
Is there a way to type a string using this value? Unicode are expected to be 4 digits and emojis got 5 so I can't manually type "\u12345" and get an emoji, since "\u1234" is already a character. See this related issue: #19557526 Text editors will display the emoji but it's confusing not to be able to "see" the underlying raw values – Tow
If I go on this site emojiterra.com/fr/antenne-satellite, for JavaScript, I'll get "\uD83D\uDCE1" for a satellite emoji, so 2 4-digit value and not 1 5-digit value. How can I obtain this value? – Tow
C
43

Two way

let hex = "πŸ˜€".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);

console.log(hex, emo);
Commercial answered 17/1, 2020 at 18:34 Comment(5)
Really appreciate having the reverse function here! – Grider
\uD83D\uDE0A sir i need to convert mentioned uniquecode to emoji? – Lindsley
@Kapilsoni try console.log( String.fromCodePoint("0xD83D","0xDE0A") ) (I get this: 😊 - but if I change sequence of this 2 strings I get οΏ½οΏ½ , if I join strings to "0xD83DDE0A" I get exception – Grimbly
@KamilKieΕ‚czewski:sir my case unicode string is \uD83D\uDE0A.if directly put \uD83D\uDE0A in fromCodePoint its not working.can i convert in any another format? – Lindsley
@AndreasM. ofcourse, however OP needs hex in question – Grimbly
G
19

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

Generality answered 24/1, 2018 at 10:11 Comment(8)
That's wrong. When you removed the returns from the original function and didn't add the proper ifs and elses you altered the function. Correct would be how it is here: raw.githubusercontent.com/IonicaBizau/emoji-unicode/… – Gloriane
What about emojis that are a combination of two others and have charCodeAt from 0 to 4? E.g.: πŸ€¦β€β™€οΈ – Agra
(Javascript), In case you need to convert it back to Emoji again use: String.fromCodePoint(parseInt ("1f600", 16)) – Zonked
Country flag doesn't render properly – Womanlike
@Parth Gajjar: sir can you tell me how to get get emoji from \uD83D\uDE0A this string? – Lindsley
Simple Heart 'β™₯' returns NaN – Muirhead
Is there a way to type a string using this value? Unicode are expected to be 4 digits and emojis got 5 so I can't manually type "\u12345" and get an emoji, since "\u1234" is already a character. See this related issue: #19557526 Text editors will display the emoji but it's confusing not to be able to "see" the underlying raw values – Tow
If I go on this site emojiterra.com/fr/antenne-satellite, for JavaScript, I'll get "\uD83D\uDCE1" for a satellite emoji, so 2 4-digit value and not 1 5-digit value. How can I obtain this value? – Tow
A
12

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);
};
Armes answered 20/4, 2019 at 15:11 Comment(3)
It seems to me this Proso (provider of solution) has the right idea. According to Mozilla developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… codePointAt is specifically for handling emojis or icons. – Bodycheck
πŸ”Ž My experimentation indicates you do not need the toString(16). If the emoji is 2 chars long (s.length >1), then the 0 position codePointAt(0), renders up the dec needed for html display. – Bodycheck
My mistake, the toString(16) is get the Hex value. – Bodycheck
L
5

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);
}
Linesman answered 24/1, 2018 at 10:10 Comment(1)
Best solution if you want to get it working in RESP API using json. It give output like "\uD83D\uDE00" (for πŸ˜€) – Wadesworth
K
4

Emojis like πŸ‘©β€βš•οΈ have two parts: πŸ‘© +βš•οΈ.
Here is how to get their code:

emoji = "πŸ‘©β€βš•οΈ" // corresponds to  1f469-200d-2695-fe0f
code1 = emoji.codePointAt(0).toString(16) // gives only 1f469
code2 = [...emoji].map(e => e.codePointAt(0).toString(16)).join(`-`) // gives correctly 1f469-200d-2695-fe0f
console.log(code1)
console.log(code2)
Kirghiz answered 9/6, 2022 at 16:19 Comment(1)
emoji_code = "-".join(f"{ord(c):x}" for c in "πŸ‘©β€βš•οΈ") would be the corresponding implementation in python. – Kirghiz
D
3

Here is another way. Source

 "πŸ˜€".codePointAt(0).toString(16)
Disposable answered 3/12, 2019 at 8:56 Comment(1)
Great answer, combine that with this data source: github.com/iamcal/emoji-data/blob/master/emoji.json and you get a great way to convert emojis to shortcuts – Heiner
W
0

Best answer in my view is to use node-emoji package.

https://www.npmjs.com/package/node-emoji

Here are steps.

  1. do npm i node-emoji

    var emoji = require('node-emoji');
    var convertEmoji = function(data){
    if(emoji.hasEmoji(data)){
        return emoji.unemojify(data);
      }
      else{
         return data;
      }
    }
    
Wampum answered 27/5, 2022 at 7:10 Comment(0)
I
0
const 
  getUnicodeHex = char => char.codePointAt(0).toString(16),    
  getEmoji = unicodeHex => String.fromCodePoint(unicodeHex)

console.log(
  getUnicodeHex('πŸ˜€'),  // 1f600
  getEmoji(0x1f600)     // πŸ˜€
)
Idempotent answered 31/1, 2023 at 16:36 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.