Converting emoji from hex code to unicode
Asked Answered
Z

3

5

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.

Zaccaria answered 6/2, 2017 at 6:13 Comment(2)
Are you using Swift? If so, then "\u{1f600}" works fine. Same for modern JavaScript. For Java, appendCodePoint should work. Can you give us more info for what you are trying to do? Or are you looking for the general algorithm to convert code points to UTF-16? – Canter
1F600 is full Unicode. D83D DE00 is UTF-16 encoding of that Unicode. You can either use the full Unicode as RayToal's comment showed, or any Unicode to UTF-16 converter, or an Emoji table that lists UTF-16 such as punchdrunker.github.io/iOSEmoji/table_html/index.html. – Heroic
C
5

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

Canter answered 6/2, 2017 at 6:29 Comment(1)
Great explanation. Thanks a lot. I was able to just use the unicode by using \u{1f600} as you mentioned in your context. I am using it in react-native which then passes the emoji code along to both Swift and Java and it worked on both of them. – Zaccaria
W
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);
Weems answered 9/8, 2019 at 11:40 Comment(1)
Welcome to SO! Thanks for your input, I adjusted the typesetting for you. – Guzel
M
1

JavaScript - two way

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

console.log(hex, emo);
Mallet answered 17/1, 2020 at 18:35 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.