// HEXADECIMAL to DECIMAL
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int n=0,POWER=0,i,j;
char ar1[20];
char ar2[16] = {'0','1','2','3','4','5','6','7','8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
printf("Enter any Hexadecimal Number : ");
scanf("%s",&ar1);
//let ar1 = A2C
for(i=strlen(ar1)-1;i>=0;i--)
//i=2,1,0
{
for(j=0;j<16;j++)
{
if(ar1[i]==ar2[j])
{
n=n+j*pow(16,POWER);
}
}
POWER++;
/*
step 1:
ar1[2]=C=ar2[12],it means j=12
n = 0+12*pow(16,0) = 0+12 = 12
POWER=1
step 2:
ar1[1]=2=ar2[2],it means j=2
n = 12+2*pow(16,1) = 12+32 = 44
POWER=2
step 2:
ar1[0]=A=ar2[10],it means j=10
n = 44+10*pow(16,2) = 44+2560 = 2604
POWER=3
n = 2604
*/
}
printf("\nDecimal Number = %d",n);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.