JackyWhite
JackyWhite

Reputation: 21

Android emoji issue(convert "\uD83D\uDE04" to 0x1F604)

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

Upvotes: 1

Views: 12940

Answers (4)

Gowtham Kumar
Gowtham Kumar

Reputation: 544

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)

Upvotes: 6

Alexiscanny
Alexiscanny

Reputation: 579

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
    }
}

Upvotes: 0

JackyWhite
JackyWhite

Reputation: 21

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;

Upvotes: 0

JackyWhite
JackyWhite

Reputation: 21

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

Upvotes: 1

Related Questions