Java Program For Hexadecimal to Decimal Conversion
Last Updated :
20 Aug, 2023
Given a Hexadecimal number N, convert N into an equivalent decimal number i.e. convert the number with base value 16 to base value 10. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.
Illustration:
Input : 1AB
Output: 427
Input : 1A
Output: 26
Approach to Convert Hexadecimal to Decimal Conversion
The approach to implementing the conversion is mentioned below:
- The idea is to extract the digits of a given hexadecimal number starting from the rightmost digit.
- Keep a variable ‘dec_value’.
- At the time of extracting digits from the hexadecimal number, multiply the digit with the proper base (Power of 16) and add it to the above variable taken that is ‘dec_value’.
- In the end, the variable ‘dec_value’ will store the required decimal number.
Programs to Convert Hexadecimal to Decimal Conversion
Below is the implementation of the above approach:
Java
import java.io.*;
class GFG {
static int hexadecimalToDecimal(String hexVal)
{
int len = hexVal.length();
int base = 1 ;
int dec_val = 0 ;
for ( int i = len - 1 ; i >= 0 ; i--) {
if (hexVal.charAt(i) >= '0'
&& hexVal.charAt(i) <= '9' ) {
dec_val += (hexVal.charAt(i) - 48 ) * base;
base = base * 16 ;
}
else if (hexVal.charAt(i) >= 'A'
&& hexVal.charAt(i) <= 'F' ) {
dec_val += (hexVal.charAt(i) - 55 ) * base;
base = base * 16 ;
}
}
return dec_val;
}
public static void main(String[] args)
{
String hexNum = "1A" ;
System.out.println(hexadecimalToDecimal(hexNum));
}
}
|
The complexity of the above method
The time complexity is O(n), where n is the length of the input hexadecimal string.
The auxiliary space is O(1), which is constant.
Algorithm to Convert a Decimal to Binary Number:
There is an algorithm that can be followed to convert a Decimal to Binary Number as mentioned below:
- Take the input decimal number.
- If the number is 0, return 0 as the binary equivalent.
- Initialize an empty string to store the binary digits.
- Divide the decimal number by 2 and get the quotient and remainder.
- Convert the remainder to a string and append it to the binary string.
- Set the decimal number to the quotient obtained in step 4.
- Repeat steps 4-6 until the decimal number becomes 0.
- Reverse the binary string obtained in steps 3-7.
- Return the reversed binary string.
This algorithm uses the division method to convert a decimal number to binary. At each step, the remainder is computed and appended to the binary string. The quotient becomes the new decimal number for the next iteration. Finally, the binary string is reversed to get the correct binary equivalent of the decimal number.
Below is the implementation of the above algorithm
Java
public class HexToDec {
public static void main(String[] args)
{
String hex1 = "1AB" ;
String hex2 = "1A" ;
int dec1 = hexToDec(hex1);
int dec2 = hexToDec(hex2);
System.out.println(hex1 + " in decimal is " + dec1);
System.out.println(hex2 + " in decimal is " + dec2);
}
private static int hexToDec(String hex)
{
int len = hex.length();
int dec = 0 ;
for ( int i = 0 ; i < len; i++) {
char c = hex.charAt(i);
int digit = Character.digit(c, 16 );
dec = dec * 16 + digit;
}
return dec;
}
}
|
Output
1AB in decimal is 427
1A in decimal is 26
The complexity of the above method
Time complexity: O(log n), where n is the input decimal number.
Auxiliary Space: O(log n),
Share your thoughts in the comments
Please Login to comment...