Java lang Integer.toHexString() Method with Examples
The Java.lang.Integer.toHexString() is a built-in function in Java which returns a string representation of the integer argument as an unsigned integer in base 16. The function accepts a single parameter as an argument in Integer data-type.
Syntax :
public static String toHexString(int num) Parameter : The function accepts a single mandatory parameter num - This parameter specifies the number which is to be converted to a Hexadecimal string. The data-type is int.
Return Value : The function returns a string representation of the int argument as an unsigned integer in base 16.
Examples:
Input : 11 Output : b Input : 12 Output : c
Program 1: The program below demonstrates the working of function.
// Java program to demonstrate working // of java.lang.Integer.toHexString() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { int l = 234; // returns the string representation of the unsigned int value // represented by the argument in binary (base 2) System.out.println("Hex string is " + Integer.toHexString(l)); l = 11; System.out.println("Hex string is " + Integer.toHexString(l)); } } |
Output:
Hex string is ea Hex string is b
Program 2: The program below demonstrates the working function when a negative number is passed.
// Java program to demonstrate // of java.lang.Integer.toHexString() method // negative number import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when negative number is passed System.out.println("Hex is " + Integer.toHexString(-10)); } } |
Output:
Hex is fffffff6
Errors and Exception: Whenever a decimal number or a string is passed as an argument, it returns an error message which says “incompatible types”.
Program 3: The program below demonstrates the working function when a string number is passed.
// Java program to demonstrate // of java.lang.Integer.toHexString() method // string number import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when negative number is passed System.out.println("Hex is " + Integer.toHexString("12")); } } |
Output:
prog.java:13: error: incompatible types: String cannot be converted to int
System.out.println("Hex is " + Integer.toHexString("12"));
Program 4: The program below demonstrates the working function when a decimal is passed.
// Java program to demonstrate // of java.lang.Integer.toHexString() method // decimal import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when decimal number is passed System.out.println("Hex is " + Integer.toHexString(12.34)); } } |
Output:
prog.java:13: error: incompatible types: possible lossy conversion from double to int
System.out.println("Hex is " + Integer.toHexString(12.34));
Recommended Posts:
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.lang.Long.valueOf() method with examples
- Java lang.Long.toBinaryString() method with Examples
- Java lang Long.toOctalString() Method with Examples
- Java.lang.InheritableThreadLocal Class with Examples
- Java.lang.string.replace() method in Java
- Java.lang.String.lastIndexOf() Method
- Java lang.Integer.toBinaryString() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


