A
A
Andrzej Wielski2015-02-20 22:39:10
JavaScript
Andrzej Wielski, 2015-02-20 22:39:10

How to convert Unified string to unicode character (emoji)?

There is a string like
emoji = '1f606';
What in unicode format (U+1F606) is a laughing emoji.
What function can convert this string into a regular unicode character for transmission to the server?
Need to get emoji variable pastebin.com/CpqQKNFY
Thanks in advance for your huge help :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2015-02-21
@wielski

1. You can use this character as it is: ""(with the default font, the character is not visible, but it is there)
2. In javascript strings, you can use unicode characters in the form "\u25B2", where exactly 4 characters follow \u. Since the unicode character "1f606" is in the extended area, and is encoded by the so-called surrogate pair.
Here from this article ( Parsing emoji Unicode in JavaScript ) I found such a method for finding surrogate pairs:

function findSurrogatePair(point) {
  // assumes point > 0xffff
  var offset = point - 0x10000,
      lead = 0xd800 + (offset >> 10),
      trail = 0xdc00 + (offset & 0x3ff);
  return [lead.toString(16), trail.toString(16)];
}

// find pair for U+1F600
findSurrogatePair(0x1f600); // ["d83d", "de00"]

// ваш символ:
findSurrogatePair(0x1f606); // ["d83d", "de06"]

So your symbol can be written as "\ud83d\ude06"
Next, you need to replace the sequence "1f606" with our symbol, this is easily done with the command
string.replace(/1f606/g, "\ud83d\ude06");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question