# C Program to Convert Hexadecimal to Decimal

In this tutorial, we will learn about how to create a program in C that converts any given Hexadecimal number by user at run-time into its equivalent decimal value. We have also created a user-defined function that also does the same job.

But before going through the program, if you are not aware of

• Decimal Number
• Hexadecimal to Decimal Conversion Process

Then refer to Hexadecimal to Decimal conversion step by step process. Now let's move on to the program.

## Hexadecimal to Decimal in C

To convert hexadecimal number to decimal number in C programming, you have to ask from user to enter the hexadecimal number to convert it into decimal number, and then display its equivalent value in decimal as output. The question is, Write a program in C that converts Hexadecimal number to decimal. The answer to this question is:

```#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int decnum=0, rem, i=0, len=0;
char hexnum;
printf("Enter any Hexadecimal Number: ");
scanf("%s", hexnum);
while(hexnum[i]!='\0')
{
len++;
i++;
}
len--;
i=0;
while(len>=0)
{
rem = hexnum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
getch();
return 0;
}
decnum = decnum + (rem*pow(16, i));
len--;
i++;
}
printf("\nEquivalent Decimal Value = %d", decnum);
getch();
return 0;
}```

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

Now supply any hexadecimal number as input say A67 and press ENTER key to see its decimal equivalent as shown in the second snapshot of the same sample run given below:

#### Program Explained

• Receive any hexadecimal number as input say 5A9
• The entered hexadecimal number gets stored in a character array hexnum[]
• Now count length of character array hexnum[] (given hexadecimal number) using while loop
• After getting length of the given hexadecimal number, decrement value of length (the variable len holds length value) by 1, that is using len-- or len=len-1. Because indexing in array starts with 0
• Initialize i with 0, because we are going to use i as exponent of 16
• Now create while loop that runs from last index to first (the 0th index) of the given character array or hexnum[]
• Therefore at first run, the condition of the while loop len>=0 or 2>=0 (as the length of given hexadecimal number 5A9 is 3, and after decrementing its value by 1, we have len variable that holds 2 as its value) evaluates to be true, therefore program flow goes inside the loop, and hexnum[len] or hexnum or 9 gets initialized to rem
• As hexnum is a character and rem is a integer type variable, therefore using rem = hexnum, the ASCII code of hexnum or 9 gets initialized to rem
• And as we all knows that ASCII code for 0-9 is 48-57 and ASCII code for A-F (corresponds to hex digits 10 to 15) is 65-70
• Therefore we have checked and initialized corresponding decimal value of given hexadecimal digit using its ASCII code. That is, if hexnum holds 9, and ASCII code for 9 is 56, therefore rem-48 or 57-48 or 9 gets initialized to rem
• Then decnum + (rem*pow(16, i)) (decnum was initialized with 0 at start of the program) or 0 + (9*pow(16, 0)) or 0 + 9*1 or 9 gets initialized to decnum
• The value of len gets decremented and becomes 1. And the value of i gets incremented and becomes 1
• Program flow again goes back to the condition of the while loop and len>=0 or 1>=0 evaluates to be true and program flow again goes inside the loop
• There doing the similar operation. That is hexnum[len] or hexnum or A gets initialized to rem. As the ASCII code for A is 65, therefore rem-55 or 65-55 or 10 (10 represents A in hexadecimal) gets initialized to rem
• And then decnum + (rem*pow(16, i)) or 9 + (10*161) or 9+160 or 169 gets initialized to decnum
• The value of len gets decremented and the value of i gets incremented
• And program flow again goes back to the condition of the loop and do the similar operation as told in above steps
• After exiting from the while loop, we have decnum variable that holds equivalent decimal value of given hexadecimal number
• Therefore print the value of decnum variable as output

### What if Hexadecimal input contains decimal ?

The program given above is only correct when user supply any hexadecimal number as input (without any decimal point). But if he/she enters any hexadecimal number that includes point (decimal point), then you have to modify the above program with the program given below. The question is, Write a program in C that converts hexadecimal (includes decimal point) to decimal. The answer to this question is:

```#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int decnum=0, decnum1=0, rem, i=0, len=0, lenTemp, dotPos=0;
float decnum2=0;
char hexnum;
printf("Enter any Hexadecimal Number: ");
scanf("%s", hexnum);
while(hexnum[i]!='\0')
{
if(hexnum[i]=='.')
dotPos = i;
len++;
i++;
}
len--;
i=0;
if(dotPos==0)
{
while(len>=0)
{
rem = hexnum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
getch();
return 0;
}
decnum = decnum + (rem*pow(16, i));
len--;
i++;
}
printf("\nEquivalent Decimal Value = %d", decnum);
}
else
{
lenTemp = dotPos-1;
while(lenTemp>=0)
{
rem = hexnum[lenTemp];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
getch();
return 0;
}
decnum1 = decnum1 + (rem*pow(16, i));
lenTemp--;
i++;
}
lenTemp = dotPos+1;
i=-1;
while(lenTemp<=len)
{
rem = hexnum[lenTemp];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
getch();
return 0;
}
decnum2 = decnum2 + (rem*pow(16, i));
lenTemp++;
i--;
}
decnum2 = decnum1+decnum2;
printf("\nEquivalent Decimal Value = %0.2f", decnum2);
}
getch();
return 0;
}```

Here is the sample run of above program. This is the final snapshot:

#### Program Explained

• Here we have used the variable dotPos that checks whether decimal point present inside the given hexadecimal number or not
• If present, then we have initialized 1 to dotPos variable, and checks later, whether dotPos variable holds its previous value (0) or not
• If it holds, then we have to apply normal conversion rules as done in first program given above, otherwise we have to deal with different rules
• In hexadecimal number input that contains decimal point, we have used two part
• One before decimal and one after decimal
• The before decimal part gets converted into its equivalent decimal value and initialized to decnum1, whereas after decimal part gets converted into its equivalent decimal value and initialized to decnum2 (floating point variable)
• Finally sum up both the variable and initialized it to a floating-point variable decnum2
• And print its value as output

### Hexadecimal to Decimal in C using User-defined Function

Now let's create the same purpose program using user-defined function named HexToDec() as shown in the program given below. The question is, Write a program in C that converts hexadecimal to decimal using user-defined function. The answer to this question is:

```#include<stdio.h>
#include<conio.h>
#include<math.h>
int HexToDecY(char hex[]);
float HexToDecN(char hex[]);
static int i, len, dotPos;
int main()
{
int decnumInt;
float decnumFlt;
char hexnum;
printf("Enter any Hexadecimal Number: ");
scanf("%s", hexnum);
while(hexnum[i]!='\0')
{
if(hexnum[i]=='.')
dotPos = i;
len++;
i++;
}
len--;
i=0;
if(dotPos==0)
{
decnumInt = HexToDecY(hexnum);
if(decnumInt!=0)
printf("\nEquivalent Decimal Value = %d", decnumInt);
}
else
{
decnumFlt = HexToDecN(hexnum);
if(decnumFlt!=0)
printf("\nEquivalent Decimal Value = %0.2f", decnumFlt);
}
getch();
return 0;
}
int HexToDecY(char hex)
{
int dec=0, rem;
while(len>=0)
{
rem = hex[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
return 0;
}
dec = dec + (rem*pow(16, i));
len--;
i++;
}
return dec;
}
float HexToDecN(char hex)
{
int dec1=0, rem, lenTemp;
float dec2=0;
lenTemp = dotPos-1;
while(lenTemp>=0)
{
rem = hex[lenTemp];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
return 0;
}
dec1 = dec1 + (rem*pow(16, i));
lenTemp--;
i++;
}
lenTemp = dotPos+1;
i=-1;
while(lenTemp<=len)
{
rem = hex[lenTemp];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
printf("\nYou've entered an invalid Hexadecimal digit");
return 0;
}
dec2 = dec2 + (rem*pow(16, i));
lenTemp++;
i--;
}
dec2 = dec1+dec2;
return dec2;
}```

Here is the final snapshot of the above program's sample run

#### Program Explained

• We have initialized the variable i, len and dotPos as static and global variable
• So that all the three variable remembers its previous value and stays known through out the program
• We have not initialized 0 to all the three static variable as static variable automatically holds 0 as its initial value
• We have used two function named HexToDecY(), and HexToDecN()
• The function HexToDecY() deals with hexadecimal input without decimal point, and the function HexToDecN() deals with hexadecimal input with decimal point
• We have supplied 65A7.98 as hexadecimal number input
• It contains a decimal point, therefore HexToDecN() function gets called and executed
• And finally it returns dec2 to the main() function that contains decimal equivalent of given hexadecimal number 65A7.98
• In main() function, value of dec2 gets initialized to decnumFlt variable, and the value of it gets printed as output

Now let's take another sample run, this time we have supplied a hexadecimal number without any decimal point as shown in the sample run given below Here is another final snapshot of the sample run:

#### Same Program in Other Languages

C Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋