1

I'm trying to use emoji in my EditText Fields. But the problem is that when I try to convert emoji into hexa the value is not correct.

I'm using this function to get emoji value in Hexa.

public static String escapeJavaString(String st){
    StringBuilder builder = new StringBuilder();
    try {
        for (int i = 0; i < st.length(); i++) {
            char c = st.charAt(i);
            if(!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)&& !Character.isWhitespace(c) ){
                String hexa= String.valueOf(c);
                int code = (int)c;
                if(!(code >= 0 && code <= 255)){
                    hexa= "0x"+Integer.toHexString(c);
                }
                builder.append(unicode);
            }
            else{
                builder.append(c);
            }
        }
        Log.i(TAG, builder.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return builder.toString();
}

I tried to add smiley face and it give me value 0xd83d Which is not work in below function.

And Here is function to convert Hexa value into Emoji, This function is working fine I tried it with providing manual Hexa value.

        // 0xd83d this value is get from above function but not work
        // in this function 
        //String hexaCode = "0xd83d"; // not work
        String hexaCode = "0x1F60A"; // work
        int emoji= Integer.parseInt(hexaCode.substring(2), 16);
        reurn new String(Character.toChars(emoji));

Can you please let me know what am I doing wrong. Please don't refer me to any emoji library.

Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
  • I thought you are asking for library , refer discussion here https://stackoverflow.com/questions/45453202/how-to-use-the-imagestored-image-of-device-with-text-on-textview-android – Omar Dhanish Aug 16 '17 at 07:49

3 Answers3

1

I solved it and this is working fine for me.
Here is code.

public String escapeJavaString(String st){
    int ss1 = 0,ss2;
    Boolean high = true;
    StringBuilder builder = new StringBuilder();
    try {
        for (int i = 0; i < st.length(); i++) {
            char c = st.charAt(i);
            if(!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)&& !Character.isWhitespace(c) ){
                String unicode = String.valueOf(c);
                int code = (int)c;
                if(!(code >= 0 && code <= 255)){
                    unicode = Integer.toHexString(c);
                    if(high){
                        high = false;
                        ss1 = Integer.parseInt(unicode, 16);
                    }else{
                        high = true;
                        ss2 = Integer.parseInt(unicode, 16);
                        char chars = Character.toChars(ss1)[0];
                        char chars2 = Character.toChars(ss2)[0];
                        int codepoint = Character.toCodePoint(chars, chars2);
                        unicode = "Ax" + codepoint;
                        builder.append(unicode);
                    }
                }
            }
            else{
                builder.append(c);
            }
        }
        Log.i(TAG, builder.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return builder.toString();
}

And For decoding use this.

    int emoji= Integer.parseInt(hexaCode.substring(2));
    reurn new String(Character.toChars(emoji));
Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
0

In escapeJavaString(String st) method you are getting unicode. You can't convert it to codePonit just adding 0x at start. Take a look at this aswer, hope you find a key

Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46
0
String str = edText.getText().toString();
String unicodeStr = StringEscapeUtils.escapeJava(str);

You can get Unicode from emoji by using commons-lang-2.5 jar

if you want to get index or hex value then first need to parse a string into HTML characters

StringEscapeUtils.escapeHtml()

or use an emoji4j library.

For example:

String line = "Hi , i am fine \uD83D\uDE02 \uD83D\uDE02, how r u ?";
EmojiUtils.hexHtmlify(line); //Hi , i am fine &#x1f602; &#x1f602;, how r u ?

here 1f602 is your hex code.

System.out.println(Character.toChars(Integer.parseInt("1f602",16)));
Kishan Donga
  • 2,851
  • 2
  • 23
  • 35