Java Integer toHexString() method
The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int i) Parameters The parameter ‘i’ represents an integer to be converted to a string. Return Value This method returns the string which represents the unsigned integer value of the argument in hexadecimal. Example 1
public class JavaIntegerToHexStringExample1 { public static void main(String[] args) { Integer val=2; String val1=Integer.toHexString(val); //Returns a string representation of the integer argument as an unsigned integer in base 16 System.out.println("Hex String : "+val1); } }Output
Hex String : 2Example 2
public class JavaIntegerToHexStringExample2 { public static void main(String[] args) { //passing negative value Integer val=-982; String val1=Integer.toHexString(val); //Returns a string representation of the integer argument as an unsigned integer in base 16 System.out.println("Hex String : "+val1); } }Output
Hex String : fffffc2aExample 3
public class JavaIntegerToHexStringExample3 { public static void main(String[] args) { //passing the maximum value Integer val=Integer.MAX_VALUE; String val1=Integer.toHexString(val); //Returns a string representation of the integer argument as an unsigned integer in base 16 System.out.println("Hex String : "+val1); } }Output
Hex String : 7fffffffExample 4
public class JavaIntegerToHexStringExample4 { public static void main(String[] args) { //passing 0 Integer val=0; String val1=Integer.toHexString(val); //Returns a string representation of the integer argument as an unsigned integer in base 16 System.out.println("Hex String : "+val1); } }Output
Hex String : 0