Thursday, 17 July 2014

C program to convert hexadecimal to decimal

/* C program to convert hexadecimal to decimal */


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

void main()
{
 int i=0,j=0,l=0,d=0,s=0;
 char hex[25];
 clrscr();
 printf("Enter an hexadecimal number:");
 scanf("%s",hex);
 l=strlen(hex);
 for(i=l-1;i>=0;i--)
    {
switch(hex[i])
{
 case '0': d=0;
 break;

 case '1': d=1;
 break;

 case '2': d=2;
 break;

 case '3': d=3;
 break;

 case '4': d=4;
 break;

 case '5': d=5;
 break;

 case '6': d=6;
 break;

 case '7': d=7;
 break;

 case '8': d=8;
 break;

 case '9': d=9;
 break;

 case 'a': d=10;
 break;

 case 'A': d=10;
 break;

 case 'b': d=11;
 break;

 case 'B': d=11;
 break;

 case 'c': d=12;
 break;

 case 'C': d=12;
 break;

 case 'd': d=13;
 break;

 case 'D': d=13;
 break;

 case 'e': d=14;
 break;

 case 'E': d=14;
 break;

 case 'f': d=15;
 break;

 case 'F': d=15;
 break;
}
    s+=(d*pow(16,j));
    j++;
 }
 printf("Decimal value=%d",s);
 getch();
}

No comments:

Post a Comment