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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 |
Hex String : 2 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 |
Hex String : fffffc2a |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 |
Hex String : 7fffffff |
Example 4
1 2 3 4 5 6 7 8 9 10 11 |
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
1 2 3 |
Hex String : 0 |