Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting emoji from hex code to unicode

Tags:

unicode

emoji

I want to use emojis in my iOS and Android app. I checked the list of emojis here and it lists out the hex code for the emojis. When I try to use the hex code such as U+1F600 directly, I don't see the emoji within the app. I found one other way of representing emoji which looks like \uD83D\uDE00. When using this notation, the emoji is seen within the app without any extra code. I think this is a Unicode string for the emoji. I think this is more of a general question that specific to emojis. How can I convert an emoji hex code to the Unicode string as shown above. I didn't find any list where the Unicode for the emojis is listed.

like image 766
Varun Gupta Avatar asked Oct 22 '25 05:10

Varun Gupta


2 Answers

It seems that your question is really one of "how do I display a character, knowing its code point?"

This question turns out to be rather language-dependent! Modern languages have little trouble with this. In Swift, we do this:

$ swift
Welcome to Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1). Type :help for assistance.
  1> "\u{1f600}"
$R0: String = "😀"

In JavaScript, it is the same:

$ node
> "\u{1f600}"
'😀'

In Java, you have to do a little more work. If you want to use the code point directly you can say:

new StringBuilder().appendCodePoint(0x1f600).toString();

The sequence "\uD83D\uDE00" also works in all three languages. This is because those "characters" are actually what Unicode calls surrogates and when they are combined together a certain way they stand for a single character. The details of how this all works can be found on the web in many places (look for UTF-16 encoding). The algorithm is there. In a nutshell you take the code point, subtract 10000 hex, and spread out the 20 bits of that difference like this: 110110xxxxxxxxxx110111xxxxxxxxxx.

But rather than worrying about this translation, you should use the code point directly if your language supports it well. You might also be able to copy-paste the emoji character into a good text editor (make sure the encoding is set to UTF-8). If you need to use the surrogates, your best best is to look up a Unicode chart that shows you something called the "UTF-16 encoding."

like image 148
Ray Toal Avatar answered Oct 24 '25 02:10

Ray Toal


In Delphi XE #$1F600 is equivalent to #55357#56832 or D83D DE04 smile.

Within a program, I use it in the following way:

const smilepage : array [1..3] of WideString =(#$1F600,#$1F60A,#$2764);
like image 42
Петр Сачек Avatar answered Oct 24 '25 04:10

Петр Сачек



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!