21

how to convert this in to this 1f600 in javascript

''.charCodeAt(0);  

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

Tom Blodget
  • 19,489
  • 2
  • 37
  • 64
Parth Gajjar
  • 1,230
  • 3
  • 11
  • 35

7 Answers7

25

Two way

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

console.log(hex, emo);
Kamil Kiełczewski
  • 71,169
  • 26
  • 324
  • 295
  • 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
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

Parth Gajjar
  • 1,230
  • 3
  • 11
  • 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
10

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
  • 3,826
  • 2
  • 24
  • 33
  • 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
  • 806
  • 1
  • 10
  • 19
  • Best solution if you want to get it working in RESP API using json. It give output like "\uD83D\uDE00" (for ) – ripreal 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
0

For emoji to unicode convertion you can use emoji-unicode package:

const emojiUnicode = require("emoji-unicode");
   
console.log(emojiUnicode(""));
// => 1f525
kgangadhar
  • 4,369
  • 4
  • 30
  • 50
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;
      }
    }
    
Pravin Kottawar
  • 63
  • 2
  • 16