# C Program to Convert Hexadecimal to Decimal

## Hexadecimal to Decimal Conversion in C

To convert hexadecimal number to decimal number 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

## C Programming Code to Convert Hexadecimal to Decimal

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

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

#include<stdio.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 ' '
printf(" Enter 32-bit Hexadecimal Number : ");
scanf("%s", hex);

decnum = convtodecnum(hex);
printf("Value in Decimal Number is %u  \n", decnum);

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;
// Find 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 Programming Language

You may also like same program in other programming languages:

Tools
Calculator