# C++ Program to Convert Hexadecimal to Decimal

## Hexadecimal to Decimal Conversion in C++

To convert a number from hexadecimal format into equivalent decimal format in C++ programming, you have to ask to the user to enter the hexadecimal number to convert it into decimal number to display the equivalent value in decimal format as shown here in the following program.

## C++ Programming Code to Convert Hexadecimal to Decimal

Following C++ program ask to the user to enter any number in hexadecimal format to convert it into decimal, then display the result on the screen:

```/* C++ Program - Hexadecimal to Decimal Conversion */

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

unsigned long convtodecnum(char hex[]);

void main()
{
clrscr();
unsigned long decnum;
char hex[9];     // 8 characters for 32-bit Hexadecimal Number and one for ' '

cout<<" Enter 32-bit Hexadecimal Number : ";
cin>>hex;

decnum = convtodecnum(hex);
cout<<"Value in Decimal Number is "<<decnum<<"\n";

getch();
}

unsigned long convtodecnum(char hex[])
{
char *hexstr;
int length = 0;
const int base = 16;     // Base of Hexadecimal Number
unsigned long decnum = 0;
int i;

// Now Find the length of Hexadecimal Number
for (hexstr = hex; *hexstr != '\0'; hexstr++)
{
length++;
}

hexstr = hex;
for (i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
{
// Compare *hexstr with ASCII values
if (*hexstr >= 48 && *hexstr <= 57)   // is *hexstr Between 0-9
{
decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
}
else if ((*hexstr >= 65 && *hexstr <= 70))   // is *hexstr Between A-F
{
decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
}
else if (*hexstr >= 97 && *hexstr <= 102)   // is *hexstr Between a-f
{
decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
}
else
{

}
}
return decnum;
}
```

When the above C++ program is compile and executed, it will produce the following result:

### Same Program in Other Language

You may like the same program in other programming languages:

Tools
Calculator