int main()
{
char *str;
clrscr();
printf("\nEnter a hexadecimal string\n");
gets(str);
printf("\nthe corresponding decimal value is %d",htoi(str));
getch();
return(0);
}
int power (int a, int b) // utility function
{
int res = 1 ;
while( b>0 )
{
res *= a ;
b--;
}
return ( res );
}
int htoi ( char* s )
{
int result = 0,new_result;
//int value =0;
int len = strlen(s);
int exp = len;
char c;
int i;
for( i=0; i<len; i++)
{
c = s[i];
if ( c >='1' && c<='9')
new_result = (int)( (c - '0') * power(16, exp-1) );
else
if (c >='A' && c<='F')
new_result = (int)( (c - 55) * power(16, exp-1) );
exp --;
result += new_result;
}
return (result);
}
No comments:
Post a Comment