In this article, you will learn how to write the Java logic for Decimal to Hexadecimal conversion. The meaning of decimal to hexadecimal conversion is converting a number from its decimal representation (base 10) to its hexadecimal representation (base 16).
Decimal Number:
In the decimal number system, we use 10 digits (0-9) to represent numbers. Each digit’s position in a decimal number represents a power of 10. For example, in the number 285, the digit 8 is in the tens place, representing 8 * 10^1, and the digit 5 is in the units place, representing 5 * 10^0.
Hexadecimal Number:
In the hexadecimal number system, we use 16 digits (0-9 followed by A-F) to represent numbers. Each digit’s position in a hexadecimal number represents a power of 16. The rightmost digit represents 16^0, the next digit to the left represents 16^1, the next digit represents 16^2, and so on. In the hexadecimal system, the digits beyond 9 are represented by the letters A-F, where A represents 10, B represents 11, and so on.
Decimal to Hexadecimal Conversion Process
Converting a decimal number to hexadecimal involves dividing the decimal number by 16 repeatedly and recording the remainders until the quotient becomes 0. For remainders greater than 9, they are replaced with the corresponding hexadecimal letters. The remainders are then read in reverse order to obtain the hexadecimal representation of the original decimal number.
For example, the decimal number 305 can be converted to hexadecimal as follows:
305 divided by 16 gives a quotient of 19 and a remainder of 1
19 divided by 16 gives a quotient of 1 and a remainder of 3
1 divided by 16 gives a quotient of 0 and a remainder of 1
Reading the remainders in reverse order, we get the hexadecimal representation: 131.
Decimal to Hexadecimal Conversion Java Programs
Here are a few different solutions for converting a decimal number to its hexadecimal equivalent in Java:
1. Using Integer.toHexString()
In this solution, the Integer.toHexString()
method is used to convert the decimal number to its hexadecimal representation. The method takes an integer as input and returns a string representing the hexadecimal value.
package com.javacodepoint.conversion;
import java.util.Scanner;
public class DecimalToHexadecimal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int decimal = scanner.nextInt();
String hexadecimal = Integer.toHexString(decimal);
System.out.println("Hexadecimal equivalent: " + hexadecimal);
}
}
OUTPUT:
Enter a decimal number: 100
Hexadecimal equivalent: 64
2. Using a loop
In this solution, a loop is used to convert the decimal number to hexadecimal. The algorithm repeatedly divides the decimal number by 16 and appends the remainder to a string. For remainders greater than 9, the corresponding hexadecimal letters (A-F) are used. The loop continues until the decimal number becomes 0.
package com.javacodepoint.conversion;
import java.util.Scanner;
public class DecimalToHexadecimal2 {
public static String convertDecimalToHexadecimal(int decimal) {
StringBuilder hexadecimal = new StringBuilder();
while (decimal > 0) {
int remainder = decimal % 16;
char hexDigit = (remainder < 10) ? (char) (remainder + '0') : (char) (remainder - 10 + 'A');
hexadecimal.insert(0, hexDigit);
decimal = decimal / 16;
}
return hexadecimal.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int decimal = scanner.nextInt();
String hexadecimal = convertDecimalToHexadecimal(decimal);
System.out.println("Hexadecimal equivalent: " + hexadecimal);
}
}
OUTPUT:
Enter a decimal number: 85
Hexadecimal equivalent: 55
3. Using recursion
In this solution, recursion is used to convert the decimal number to hexadecimal. The algorithm recursively divides the decimal number by 16 and concatenates the remainder to the result of the recursive call. For remainders greater than 9, the corresponding hexadecimal letters (A-F) are used.
package com.javacodepoint.conversion;
import java.util.Scanner;
public class DecimalToHexadecimal3 {
public static String convertDecimalToHexadecimal(int decimal) {
if (decimal == 0) {
return "0";
} else {
int remainder = decimal % 16;
char hexDigit = (remainder < 10) ? (char) (remainder + '0') : (char) (remainder - 10 + 'A');
return convertDecimalToHexadecimal(decimal / 16) + hexDigit;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int decimal = scanner.nextInt();
String hexadecimal = convertDecimalToHexadecimal(decimal);
System.out.println("Hexadecimal equivalent: " + hexadecimal);
}
}
OUTPUT:
Enter a decimal number: 116
Hexadecimal equivalent: 074