Java Convert integer to hex integer

The easiest way is to use Integer.toHexString(int)


Another way to convert int to hex.

String hex = String.format("%X", int);

You can change capital X to x for lowercase.

Example:

String.format("%X", 31) results 1F.

String.format("%X", 32) results 20.


public static int convert(int n) {
  return Integer.valueOf(String.valueOf(n), 16);
}

public static void main(String[] args) {
  System.out.println(convert(20));  // 32
  System.out.println(convert(54));  // 84
}

That is, treat the original number as if it was in hexadecimal, and then convert to decimal.

Tags:

Hex

Java

Integer