Use C programing language to write a function htoi(s), which converts a string of hexadecimal digits (including an optimal 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f and A through F.
Could you please write down the full CODE for this matter! thanks much
Use C programing language to write a function htoi(s), which converts a string of hexadecimal digits to int.?
int ftoi(char[] s)
{
int len = sizeof(s)/sizeof(char);
int value = 0;
int base =1;
int charInt;
for(int i=len-1;i%26gt;=0;i--)
if( s[i]%26gt;='0' %26amp;%26amp; s[i]%26lt;='9')
charInt = s[i]-'0';
else if(s[i]%26gt;='a' %26amp;%26amp; s[i]%26lt;='f')
charInt = s[i]-'a' +10;
else if(s[i]%26gt;='A' %26amp;%26amp; s[i]%26lt;='F')
charInt = s[i]-'A' +10;
else
return -1;
value += base*charInt;
base *= 16;
}
return value;
}
blazing star
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment