Hexadecimal to Decimal Conversion

Hexadecimal to Decimal Conversion” is one of the classic programming problem exercise. Here, we are given a hexadecimal number, entered by user and our task is to write a program to convert the hexadecimal number to its equivalent decimal number.

Hexadecimal Numbers uses 16 values to represent the number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F

Examples:

Hexadecimal = A
Decimal = 15
Hexadecimal = 4E
Decimal = 78

The steps required to convert hexadecimal number into decimal number is as follows:

  1. Take Iterator = 1 and Decimal = 0.
  2. Read the given hexadecimal string in reverse order character by character.
  3. If the character encountered is digit between 0 to 9, then convert it into number by subtracting 48 from it and store the result into temp.
  4. Else if the character encountered is between ‘A’ to ‘F’, then convert it into number by subtracting 55 from it and store the result into temp. 
  5. Decimal = Decimal + temp*iterator.
  6. Multiply the iterator by 16.
  7. Repeat the steps from 2 to 6 for all the characters of the hexadecimal string.
  8. Print the result.

C++ Program to convert Hexadecimal to Decimal is as follows:

/* C++ Program to Convert Hexadecimal Number to Decimal Number */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Hexadecimal Number */  
    string Hexadecimal;  
    cout<<"Enter the Hexadecimal Number: ";  
    cin>>Hexadecimal;  
      
    /* Converting Hexadecimal to Decimal*/  
    int Decimal = 0;  
    int iterator = 1;  
    int size = Hexadecimal.length();  
    for(int i = size - 1; i >= 0; i--)  
    {  
        int temp;  
        if(Hexadecimal[i]>='A' && Hexadecimal[i]<='F')  
        {  
            temp = Hexadecimal[i] - 55;  
        }  
        else  
        {  
            temp = Hexadecimal[i] - 48;  
        }  
        Decimal = Decimal + temp*iterator;  
        iterator = iterator * 16;  
    }  
      
    /*Printing the result */  
    cout<<"The Decimal of "<<Hexadecimal<<" is "<<Decimal;  
}  

OUTPUT:
Enter the Hexadecimal Number: 4E
The Decimal of 4E is 78

Related Posts:

  1. Program to convert hexadecimal number to binary number.
  2. Program to convert hexadecimal number to octal number.
  3. Program to check Leap Year.
  4. Program to convert binary number to octal number.
  5. Program to convert binary number to decimal number.
  6. Program to convert binary number to hexadecimal number.
  7. Program to convert octal number to binary number.
  8. Program to convert octal number to decimal number.
  9. Program to convert octal number to hexadecimal number.
  10. Program to convert decimal number to binary number.
  11. Program to convert decimal number to octal number.
  12. Program to convert decimal number to hexadecimal number.
  13. Program to find largest amongst three numbers.
  14. Program to find factorial of a number.
  15. Program to find GCD of two numbers.
  16. Program to find LCM of two numbers.
  17. Program to check whether entered number is odd or even.
  18. Program to check whether entered number is prime number or not.
  19. Program to check whether entered number is palindrome or not.
  20. Program to check whether entered number is Armstrong Number or Not.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *