Search This Blog

Program to Convert Hexadecimal to Decimal - Hexadecimal Conversion Program

Hexadecimal number system is base 16 number system in which the digits used are 0 to 9 and A to F. Here is a c program to convert hexadecimal number to decimal number system.


#include<stdio.h>
#include<string.h>

int HexToDec(char* hex)
{
int dec=0,base=1,i,d;
char c;
i=strlen(hex)-1;
while(i>=0)
{
c=*(hex+i);
if(c>='A'&&c<='F')
d=10+c-'A';
else if(c>='a'&&c<='f')
d=10+c-'a';
else
d=c-'0';
dec+=d*base;
base*=16;
i--;
}
return dec;
}

void main()
{
char input[20];
printf("\nEnter the hexadecimal number\n");
scanf("%s",&input);
printf("Corresponding decimal number is: %d",HexToDec(input));
}

No comments: