C++ Program to Convert Hexadecimal to Decimal

This is a C++ Program to Convert a Given Hexadecimal Number to its Decimal Equivalent.

Problem Description

The program takes a hexadecimal number and converts it into its decimal equivalent.

Problem Solution

1. The program takes a hexadecimal number.
2. Its length is stored in a variable using a string function.
3. Using a for loop, every character is converted to its decimal equivalent by multiplying with powers of 16.
4. The result is printed.
5. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Convert a Given Hexadecimal Number to its Decimal Equivalent. The program output is shown below.

  1. #include<iostream>
  2. #include<string.h>
  3. #include<math.h>
  4. using namespace std;
  5. int main ()
  6. {
  7.     char num[20];
  8.     int i, r, len, hex = 0;
  9.     cout << "Enter a hexadecimal number : ";
  10.     cin >> num;
  11.     len = strlen(num);
  12.     for (i = 0; num[i] != '\0'; i++)
  13.     {
  14.         len--;
  15.         if(num[i] >= '0' && num[i] <= '9')
  16.             r = num[i] - 48;
  17.         else if(num[i] >= 'a' && num[i] <= 'f')
  18.                 r = num[i] - 87;
  19.              else if(num[i] >= 'A' && num[i] <= 'F')
  20.                     r = num[i] - 55;
  21.         hex += r * pow(16,len);
  22.     }
  23.     cout << "\nDecimal equivalent of " << num << " is : " << hex;
  24.     return 0;
  25. }
Program Explanation

1. The user is asked to enter a hexadecimal number and its value is stored in the character variable ‘num’.
2. The variable ‘hex’ is initialized as 0 and the length of num is stored in the variable ‘len’.
3. Using a for loop, len is decremented and every character is checked.
4. If it lies between 0 to 9, 48 is subtracted from the ASCII value and stored in r.
5. Else if it lies between ‘a’ to ‘f’, 87 is subtracted, else 55 is subtracted.
6. r is multiplied with 16 power len and stored in hex.
7. The loop continues till the string reaches the end or null character is encountered.
8. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter a hexadecimal number : f
Decimal equivalent of f is : 15
 
Case 2 :
Enter a hexadecimal number : 74
Decimal equivalent of 74 is : 116  
 
Case 3 :
Enter a hexadecimal number : DEF
Decimal equivalent of DEF is : 3567

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.