android int to hex converting android int to hex converting android android

android int to hex converting


Documentation says Integer.toHexString returns the hexadecimal representation of the int as an unsigned value.

I believe Integer.toString(value, 16) will accomplish what you want.


public static int convert(int n) {  return Integer.valueOf(String.valueOf(n), 16);} // in onstart: Log.v("TAG", convert(20) + "");  // 32 Log.v("TAG", convert(54) + "");  // 84

From: Java Convert integer to hex integer


Both Integer.toHexString, as well as String.format("%x") do not support signs. To solve the problem, you can use a ternary expression:

    int int_value = -13516;    String hex_value = int_value < 0                       ? "-" + Integer.toHexString(-int_value)                       : Integer.toHexString(int_value);