I can display emoji in textview by this way how set emoji by unicode in android textview , but how to convert something like "uD83D\uDE04" to the code point 0x1F604("uD83D\uDE04" represent 0x1F604)?
答案 0 :(得分:1)
我找到了一种方法:java.lang.Character.toCodePoint(char high,char low)
int ss1 = Integer.parseInt("d83d", 16);
int ss2 = Integer.parseInt("de04", 16);
char chars = Character.toChars(ss1)[0];
char chars2 = Character.toChars(ss2)[0];
int codepoint = Character.toCodePoint(chars, chars2);
String emojiString = new String(Character.toChars(codepoint));
答案 1 :(得分:1)
做这样的事情。
将UTF-16转换为UTF-8 `
String text = new String("uD83D\uDE04".getBytes(), StandardCharsets.UTF_8);`
获取代码点
int codepoint = text.codePointAt(0);
将其转换为Unicode
String yourUnicode="U+"+Integer.toHexString(codepoint)
答案 2 :(得分:0)
感谢@ Henry,我发现很容易获得emojiString:
String ss1 = "d83d";
String ss2 = "de04";
int in1 = Integer.parseInt(ss1, 16);
int in2 = Integer.parseInt(ss2, 16);
String s1 = Character.toString((char)in1);// http://stackoverflow.com/questions/5585919/creating-unicode-character-from-its-number
String s2 = Character.toString((char)in2);
String emojiString = s1+s2;
答案 3 :(得分:0)
我通过创建此方法来修复它:
fun encodeEmoji(message: String): String {
try {
val messageEscape = StringEscapeUtils.escapeEcmaScript(message)
val chars = Character.toChars(
Integer.parseInt(messageEscape
.subSequence(2, 6).toString(), 16))[0]
val chars2 = Character.toChars(
Integer.parseInt(messageEscape
.subSequence(8, messageEscape.length).toString(), 16))[0]
val codepoint = Character.toCodePoint(chars, chars2)
return Integer.toHexString(codepoint)
} catch (e: UnsupportedEncodingException) {
return message
}
}