In situations where mathematical calculations are needed in Java, developers might face memory limitations, especially when dealing with multiple records. In such scenarios, the hexadecimal values come into effect that utilize less memory to contain more numbers. Also, their minimal size makes input-output handling convenient as compared to the other numbering formats.
This tutorial explains the methodologies to convert an integer to hexadecimal in Java.
How to Convert Integer to Hexadecimal in Java
For converting integers to hexadecimal in Java, consider the below-given approaches:
- “toString()” Method.
- “toHexString()” Method.
- Custom Logic.
Method 1: Convert Integer to Hexadecimal Using the “toString()”
The “toString()” method retrieves a string against the passed value and returns a “NullPointerException” if the provided value is null. This method can be utilized to transform the specified integer to hexadecimal based on the corresponding base.
Syntax
String toString(int x, int bs)
In this syntax, “x” represents the integer to be converted to a string, and “bs” indicates the base to be utilized for representing the strings.
Example: Integer to Hexadecimal Conversion Using toString()
Consider the below-stated example that uses the “toString()” method to apply the discussed conversion:
public class Inthex {
public static void main(String args[]){
String conv1 = Integer.toString(44, 16);
String conv2 = Integer.toString(12, 16);
System.out.println("Hexadecimal Value -> " +conv1);
System.out.println("Hexadecimal Value -> " +conv2);
}}
In this snippet of code, simply apply the “Integer.toString()” method to convert the specified integer value, passed as the first argument, into hexadecimal representation using the specified base, i.e., 16.
Output
As seen, the hexadecimal value in both cases is retrieved accordingly.
Method 2: Convert Integer to Hexadecimal in Java Using the “toHexString()”
The “Integer.toHexString()” method retrieves a string representation of the passed argument i.e., an integer in the form of an unsigned integer in base 16. Also, the “0123456789abcdef” characters are utilized as hexadecimal digits. This method is dedicated to the required conversion and converts accordingly.
Syntax
String toHexString(int x)
In this syntax, “x” corresponds to the integer to be converted to a hexadecimal string.
Example: Integer to Hexadecimal Conversion Using toHexString()
Now, move on to the practical implementation of the discussed method to transform an integer to hexadecimal:
public class Inthex {
public static void main(String[] args) {
int intValue = 44;
System.out.println("Integer Value -> "+intValue);
System.out.println("Hexadecimal Value -> " + Integer.toHexString(intValue));
}}
This code initializes the integer value and applies the “Integer.toHexString()” method to convert the defined integer passed as its argument to hexadecimal.
Output
Method 3: Convert Integer to Hexadecimal Via Custom Logic
This demonstration converts the passed integer values into the corresponding hexadecimal values via a user-defined function:
public class Inthex {
public static String toHex(int decimal){
int x;
String hexadecimal= "";
char hexValues[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(decimal>0) {
x= decimal%16;
hexadecimal= hexValues[x]+ hexadecimal;
decimal= decimal/16;
}
return hexadecimal;
}
public static void main(String args[]){
System.out.println("Hexadecimal of 44 -> "+toHex(44));
System.out.println("Hexadecimal of 10 -> "+toHex(10));
}}
In this code, perform the below-stated steps:
- First of all, define the function “toHex()”. The function parameter corresponds to the passed integer to be evaluated.
- In its definition, create a character array that represents the hexadecimal digits to be utilized in the conversion.
- After that, divide the passed integer by “16” and convert the remainder of the division to a hexadecimal digit with the help of the defined array.
- In the “main”, invoke the defined function by passing the given integer values to be transformed to hexadecimal.
Algorithm
44/16 = 32 -> 2(Quotient)
Remainder = 12 ->C(Hexadecimal)
Hexadecimal = 2C
In this algorithm, “44” refers to the passed integer.
Output
Conclusion
To convert an integer to hexadecimal in Java, apply the “Integer.toString()” method, the “toHexString()” method, or use the custom logic. Hexadecimal values utilize less memory to contain more numbers. This blog has stated different methodologies to transform an integer to hexadecimal in Java.