1

I tried using this solution but still didn't get the result I wanted. Please help

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

I added listener to the edittext and through keyboard typed an Emoji and then using a function got its "escape java code". Ref.

public void afterTextChanged(Editable editable) {
  String text = editable.toString();

     try {

            Log.e("NEWW", "Val "+encodeMessage(text));

        } catch (Exception e) {
            e.printStackTrace();
        }
}

private String encodeMessage(String message) {
        message = message.replaceAll("&", ":and:";
        message = message.replaceAll("\\+", "lus:";
        return StringEscapeUtils.escapeJava(message);
    }

Now I want to get this emoji's Unicode which is in format 0x1FXXX.

Mr.Code
  • 31
  • 4
  • Please show *exactly* what you tried, and the *exact* result. Note that U+0620 is the Arabic Letter Kashmiri Yeh. Is that actually what you wanted? (That's not a character that you'd represent with a surrogate pair...) – Jon Skeet Sep 26 '22 at 13:24
  • I am trying to convert a emoji (say Angry Face) to its Unicode counterpart. Angry Face in escape java code is "\uD83D\uDE20" and unicode is U+1F620 (My Bad) – Mr.Code Sep 26 '22 at 13:59
  • You'll need to provide more information than that - and do so **in the question** (not just in comments). (We have no idea of what representation you already have, or what you want.) I suspect you should edit your question title to refer to U+1F620 instead of U+620 as well, as it doesn't make sense at the moment. – Jon Skeet Sep 26 '22 at 14:01
  • done..please check – Mr.Code Sep 26 '22 at 14:13
  • Except you haven't told us anything about what you've seen in the logs, or what the replacements are there for... it's *really* hard to help with only partial information, and having to ask multiple times for full information wastes everyone's time. – Jon Skeet Sep 26 '22 at 14:43
  • (Perhaps you're just looking for `String.codePointAt()`? That would give you the Unicode code point as an integer...) – Jon Skeet Sep 26 '22 at 14:44

1 Answers1

0

If I understand you correctly, what you're really after is a UTF-32 Big Endian. So here is an example:

public static int toCodePoint(char high, char low) {
    return ((high - 0xD800) << 10) + (low - 0xDC00) + 0x010000;
}

//In main method.
String str = "\uD83D\uDE20";
String cp = String.format("%X", toCodePoint(str.charAt(0), str.charAt(1)));
System.out.println(cp); //Prints 1F620
Darkman
  • 2,941
  • 2
  • 9
  • 14