Converting emoji from hex code to unicode

早过忘川 提交于 2019-12-22 01:18:57

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/42061358/converting-emoji-from-hex-code-to-unicode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!