1

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

Community
  • 1
  • 1
JackyWhite
  • 21
  • 1
  • 1
  • 4
  • See here for details https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates – Henry Apr 25 '16 at 09:01

4 Answers4

5

Do something like this.

Convert UTF-16 to UTF-8

String text = new String("uD83D\uDE04".getBytes(), StandardCharsets.UTF_8);

Get the code point

int codepoint = text.codePointAt(0);

Convert it to Unicode

String yourUnicode="U+"+Integer.toHexString(codepoint)
Community
  • 1
  • 1
Gowtham Kumar
  • 534
  • 8
  • 22
1

I find a way: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));
JackyWhite
  • 21
  • 1
  • 1
  • 4
0

Thanks to @Henry,I find a easy to get 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;
JackyWhite
  • 21
  • 1
  • 1
  • 4
0

I fix it by creating this method:

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
    }
}
Alexiscanny
  • 581
  • 7
  • 15