**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.

**Also See:** Program on Decimal to Hexadecimal Number Conversion

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

- Get the last digit of the hex number, call this digit the currentDigit.
- Make a variable, let’s call it power. Set the value to 0.
- Multiply the current digit with (16^power), store the result.
- Increment power by 1.
- Set the the currentDigit to the previous digit (2
^{nd}last digit, then 3^{rd}last and so on) of the hex number. - Repeat from step 3 until all digits have been multiplied.
- 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 1C2 (16^1) means16, and ^{1}(16^2) means 16, and so on.^{2} |

2 x (16^1) | 32 |
Process the previous, which is |

C x (16^2) | 3072 |
Process the previous digit, which is |

1 x (16^3) | 4096 |
Process the previous digit, which is |

Answer |
7208 |
This number comes from the |

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 *** **16 ^{3} + 12 * 16^{2} + 2 * 16^{1} + 8 * 16^{0
}**

**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(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a Hexa-Decimal number : "); String hex = br.readLine(); hex = hex.toUpperCase(); ob.hex2Dec(hex); } }

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

- We have inputted the hexadecimal number as a String since it can contain characters from A – F
- Extracting the last digits of the hexadecimal number, we are saving it as a character in the variable ‘
**curDig**‘ - 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**‘ - 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(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a Hexa-Decimal number : "); String hex = br.readLine(); hex = hex.toUpperCase(); ob.hex2Dec(hex); } }

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

- We have inputted the hexadecimal number as a String since it can contain characters from A – F
- Extracting the last digits of the hexadecimal number, we are saving it as a character in the variable ‘
**curDig**‘ - 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**‘ - 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.]