Varun Gupta
Varun Gupta

Reputation: 3112

Converting emoji from hex code to unicode

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.

Upvotes: 5

Views: 9722

Answers (3)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92347

JavaScript - two way

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

console.log(hex, emo);

Upvotes: 1

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);

Upvotes: 1

Ray Toal
Ray Toal

Reputation: 88378

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."

Upvotes: 5

Related Questions