0

I can see emojis if I enter unicode directly and convert it to String with a method like:

int intUnicode = 0x1F601;

public String getEmojiByUnicode(int unicode) {
    return new String(Character.toChars(unicode));
}

But I just can't wrap my head around how to convert when the unicode value is a String like:

String strUnicode = "0xf601";

How to covert it to an Integer like I did it directly. When I try to use Integer.parseInt(strUnicode) compiler throws an error every time.

InputStream is = assetManager.open("emoji_unicode.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is != null){
    try {
        while ((data = reader.readLine()) != null){
            sbuffer.append(data + "\n");
            String j = sbuffer.toString().trim();
            int k = Integer.parseInt(j,16);
            String l = getEmojiByUnicode(k);
            emoArray.add(l);
        }
        btn.setText(emoArray.get(5));
        is.close();
    }catch (IOException e){
        e.printStackTrace();
    }
}

It's not very hard, I know I'm very close but still it won't get to me. Any help would be very appreciated. Regard

Michael
  • 57,169
  • 9
  • 80
  • 125
Meimo
  • 101
  • 8
  • Your unicode string that you're starting with isn't the same as the one you generate in your first example. You should look at the [UTF-16](https://en.wikipedia.org/wiki/UTF-16) encoding, and see how it applies to code points in [the range you're working with](https://en.wikipedia.org/wiki/UTF-16#U+10000_to_U+10FFFF). Your compiler is complaining because the string you're giving it isn't valid UTF-16. – Jules Dec 09 '17 at 19:02
  • Possible duplicate of [Parsing a Hexadecimal String to an Integer throws a NumberFormatException?](https://stackoverflow.com/questions/11377944/parsing-a-hexadecimal-string-to-an-integer-throws-a-numberformatexception) – Remy Lebeau Dec 13 '17 at 02:58

1 Answers1

0

You're attempting to parse a hex string (unicode "encodings" are hexadecimal numbers), and Integer.parseInt() has some odd limits for doing so. Take a look at this answer:

Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42