M.Hadi M.Hadi - 2 months ago 17
Android Question

Integer.parseInt("0x1F60A") ends up with NumberformatException

I try to get the emoji code inside a long string from database,in this format: 0x1F60A ... So I can access the code but it will be a String.

At first,I tried to cast the variable by doing

tv.setText(beforeEmo + getEmijoByUnicode((int)emoKind));
but Android Studio hints: "cannot cast 'java.lang.String' to int"...
the "getEmijoByUnicode" method is:
public String getEmijoByUnicode(int unicode) {
return new String(Character.toChars(unicode));
}


so I tried this one:
tv.setText(beforeEmo + getEmijoByUnicode(Integer.parseInt(emoKind)));
but it crashes with NumberFormatError. Is there any way to make the emoji appear in my text?

Answer

Try

Integer.parseInt("1F60A", 16);

or

Long.parseLong("1F60A", 16);

to convert the string to int or long. So you have to get rid of the "0x", like this

getEmijoByUnicode(Integer.parseInt(emoKind.substring(2), 16));
Comments