Open In App

Java lang Integer.toHexString() Method with Examples

Last Updated : 05 Dec, 2018
Comments
Improve
Suggest changes
5 Likes
Like
Report
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.
Output:
Hex string is ea
Hex string is b
Program 2: The program below demonstrates the working function when a negative number is passed.
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.
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.
Output:
prog.java:13: error: incompatible types: possible lossy conversion from double to int
    System.out.println("Hex is " + Integer.toHexString(12.34)); 

Next Article
Practice Tags :

Similar Reads