JAVA – How To Convert Integer (INT) To Hexidecimal (HEX)

Integer to Hexidecimal string:

int intVal = 123;
String hexVal = Integer.toHexString(intVal);
System.out.println(“Hexidecimal: ” + hexVal);


Hexidecimal: 7b

Hexidecimal string to Integer:

String hexVal = “7b”;
int intVal = Integer.parseInt(hexVal,16);
System.out.println(“Integer: ” + intVal);Output:


Integer: 123

How To Convert A Float Value to An Integer

JAVA – how to convert a float value to an interger.

Example 1:

float a = 110.25;
int a = (int)(a + 0.5f);

int a will be rounded down version of float a. The value will be 110.

Example 2:

float b = 110.75;
int b = (int)(b + 0.5f);

int b will be rounded up version of float b. The value will be 111.