Saturday , 5 December 2020
Latest Posts
Home » Class 10 » Program on Hexadecimal to Decimal Number Conversion

# Program on Hexadecimal to Decimal Number Conversion

#### Question:

Write a Program in Java to input a number in Hexadecimal number system and convert it into its equivalent number in the Decimal number system.

Note: Hexadecimal Number system is a number system which can represent a number in any other number system in terms of digits ranging from 0 to 9 and then A – F only. This number system consists of only sixteen basic digits i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Here 10 is represented as A, 11 as B and so on till 15 which is represented as F.

#### Steps to convert a Hexadecimal number to a Decimal number:

1. Get the last digit of the hex number, call this digit the currentDigit.
2. Make a variable, let’s call it power.  Set the value to 0.
3. Multiply the current digit with (16^power), store the result.
4. Increment power by 1.
5. Set the the currentDigit to the previous digit (2nd last digit, then 3rd last and so on) of the hex number.
6. Repeat from step 3 until all digits have been multiplied.
7. Sum the results of step 3 to get the answer number.

Example :

Convert the Hexadecimal number 1C28 to Decimal

 Multiplication Result Notes 8 x (16^0) 8 Start from the last digit of the number.  In this case, the number is 1C28.  The last digit of that number is 8.  Note that any number the power of 0 is always 1. Also note the notation (16^0) means 160, and (16^1) means161, and (16^2) means 162, and so on. 2 x (16^1) 32 Process the previous, which is 2.  Multiply that number with an increasing power of 16. C x (16^2) 3072 Process the previous digit, which is C, note that C = 12 1 x (16^3) 4096 Process the previous digit, which is 1. After this we stop because there’s no more digit to process Answer 7208 This number comes from the sum of the Results (8+32+3072+4096)=7208

Notice that the above process is essentially performing this calculation:
1x(16^3) + Cx(16^2) + 2x(16^1) + 8x(16^0)
i.e. 1 * 163 + 12 * 162 + 2 * 161 + 8 * 160

#### Solution [Method 1]:

```/**
* The class HexaToDecimal inputs a Hexadecimal number and converts it into its equivalent Decimal number
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class HexaToDecimal
{
void hex2Dec(String hex)
{
int l = hex.length();
char curDig;
long dec = 0L;
int d = 0, power = 0;
for(int i=l-1; i>=0; i--) //Running loop backward to extract digits from the end
{
curDig = hex.charAt(i);
if(curDig>='0' && curDig<='9')
d = curDig-48;
if(curDig>='A' && curDig<='F')
d = curDig-55;

dec = dec + d*(long)Math.pow(16,power);
power++;
}
System.out.println("The number in Decimal System = "+dec);

}

public static void main(String args[])throws IOException
{
HexaToDecimal ob = new HexaToDecimal();
System.out.print("Enter a Hexa-Decimal number : ");
hex = hex.toUpperCase();
ob.hex2Dec(hex);
}
}
```

#### Some Points to note regarding the above solution:

1. We have inputted the hexadecimal number as a String since it can contain characters from A – F
2. Extracting the last digits of the hexadecimal number, we are saving it as a character in the variable ‘curDig
3. Since, the extracted digits are in character data-type, we need to convert it into its equivalent integer. To do this, we are subtracting 48 (ASCII value of ‘0’) if the extracted character is from ‘0’ – ‘9’. Similarly, we are subtracting 55 if the extracted character is from ‘A’ – ‘F’ because ASCII value of ‘A’ is 65, and we need to convert it into integer 10.
The equivalent integer is being stored in the variable ‘d
4. We are then multiplying this integer with 16 raised to the power of (0 or 1 or 2 etc accordingly as illustrated above) and adding the result to a variable ‘dec’

#### Solution [Method 2]:

```/**
* The class HexaToDecimal inputs a Hexadecimal number and converts it into its equivalent Decimal number
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class HexaToDecimal
{
void hex2Dec(String hex)
{
int l = hex.length();
char curDig;
long dec = 0L;
int d = 0, power = 0;
String s = "0123456789ABCDEF";
for(int i=l-1; i>=0; i--) //Running loop backward to extract digits from the end
{
curDig = hex.charAt(i);
d = s.indexOf(curDig);
dec = dec + d*(long)Math.pow(16,power);
power++;
}
System.out.println("The number in Decimal System = "+dec);

}

public static void main(String args[])throws IOException
{
HexaToDecimal ob = new HexaToDecimal();
System.out.print("Enter a Hexa-Decimal number : ");
hex = hex.toUpperCase();
ob.hex2Dec(hex);
}
}
```

#### Some Points to note regarding the above solution:

1. We have inputted the hexadecimal number as a String since it can contain characters from A – F
2. Extracting the last digits of the hexadecimal number, we are saving it as a character in the variable ‘curDig
3. Since, the extracted digits are in character data-type, we need to convert it into its equivalent integer. To do this, we are finding index of the character from the String ‘s’.
Index Of ‘7’ is 7, index of ‘A’ is 10 and likewise.
The equivalent integer is being stored in the variable ‘d
4. We are then multiplying this integer with 16 raised to the power of (0 or 1 or 2 etc accordingly as illustrated above) and adding the result to a variable ‘dec’

#### Output:

1. Enter a Hexa-Decimal number : 4DB
The number in Decimal System = 1243

2. Enter a Hexa-Decimal number : 1C28
The number in Decimal System = 7208

[Note: Don’t forget to share these resources and links from our website in your social networking sites with your friends and followers.]

## [Question 1] ISC 2019 Computer Practical Paper Solved – Future Date

Solution of Program 1 of ISC 2019 Computer Science Paper 2 (Practical) Exam. Java program to find the future date.