sayres kabir sayres kabir -4 years ago 149
Android Question

change hex of integer resource to String

I am new in android.
in my value resource i create an xml layout and put this line in to it:

<integer name="mode_happy"> 0x1F60A</integer>


in my activity I want convert 0x1F60A to String. for this i create a method:

private String getStringOfEmojiCode(int emogiCode) {
StringBuilder sb = new StringBuilder();
//convert hex to char
sb.append(Character.toChars(emogiCode));
return sb.toString();
}


when I pass
mode_happy
to my method:

mSelectedMode =
getStringOfEmojiCode(getResources().getInteger(R.integer.mode_happy));


I receive this :
mSelectedMode: ��


but i want to get like this:
0x1F60A

where is my mistake?

Answer Source

A formatter may be used to build the string from hex characters in a particular format:

String mSelectedMode 
       = String.format("0x%05X", getResources().getInteger(R.integer.mode_happy));

The format 0x%05X takes in a 5 digit hex character signified by 05X with 'X' indicating hex. A lowercase x may be used which in this case would give out the output: 0x1f60a. The 0x at the beginning can be thought of as a prefix to the argument. Refer to this for further details on formatting.

This worked for me, the hex which you stored as an integer was being converted to a decimal for me, but using the formatter worked. This also meant I did not have to use any other method. Does this solve your problem?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download