23

how to convert this into this 1f600 in javascript

''.charCodeAt(0);  

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

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Parth Gajjar
  • 1,334
  • 3
  • 12
  • 35

8 Answers8

41

Two way

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

console.log(hex, emo);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • 4
    Really appreciate having the reverse function here! – rgbflawed Mar 16 '20 at 15:06
  • \uD83D\uDE0A sir i need to convert mentioned uniquecode to emoji? – Kapil Soni Jun 25 '20 at 12:35
  • @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 – Kamil Kiełczewski Jun 25 '20 at 13:47
  • @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? – Kapil Soni Jun 25 '20 at 14:14
18

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

Parth Gajjar
  • 1,334
  • 3
  • 12
  • 35
  • 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: https://raw.githubusercontent.com/IonicaBizau/emoji-unicode/c6e9b4f4cfb4546a71aa93ea59dd0f38c057c9f3/lib/index.js – rafaelgomesxyz May 15 '18 at 21:46
  • 4
    What about emojis that are a combination of two others and have charCodeAt from 0 to 4? E.g.: ‍♀️ – Bruno Lemos Jan 31 '19 at 14:37
  • (Javascript), In case you need to convert it back to Emoji again use: String.fromCodePoint(parseInt ("1f600", 16)) – Martin Lloyd Jose Mar 12 '19 at 04:16
  • Country flag doesn't render properly – Anil Jun 06 '19 at 07:31
  • @Parth Gajjar: sir can you tell me how to get get emoji from \uD83D\uDE0A this string? – Kapil Soni Jun 25 '20 at 12:28
  • Simple Heart '♥' returns NaN – Sodj Oct 26 '22 at 10:11
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);
};
Vad
  • 4,052
  • 3
  • 29
  • 34
  • It seems to me this Proso (provider of solution) has the right idea. According to Mozilla https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt codePointAt is specifically for handling emojis or icons. – lucsan Jun 06 '19 at 09:00
  • 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. – lucsan Jun 06 '19 at 09:07
  • My mistake, the toString(16) is get the Hex value. – lucsan Jun 06 '19 at 13:54
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);
}
Mitul Gedeeya
  • 886
  • 1
  • 10
  • 20
  • Best solution if you want to get it working in RESP API using json. It give output like "\uD83D\uDE00" (for ) – the one Dec 17 '20 at 11:12
3

Here is another way. Source

 "".codePointAt(0).toString(16)
  • Great answer, combine that with this data source: https://github.com/iamcal/emoji-data/blob/master/emoji.json and you get a great way to convert emojis to shortcuts – vvo Apr 01 '21 at 13:25
1

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)
Kolibril
  • 1,096
  • 15
  • 19
  • `emoji_code = "-".join(f"{ord(c):x}" for c in "‍⚕️")` would be the corresponding implementation in python. – Kolibril Jun 09 '22 at 16:22
0
const 
  getUnicodeHex = char => char.codePointAt(0).toString(16),    
  getEmoji = unicodeHex => String.fromCodePoint(unicodeHex)

console.log(
  getUnicodeHex(''),  // 1f600
  getEmoji(0x1f600)     // 
)
UniParse
  • 21
  • 4
-1

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;
      }
    }
    
Pravin Kottawar
  • 71
  • 2
  • 18