Sunday, August 2, 2009

Use C programing language to write a function htoi(s), which converts a string of hexadecimal digits to int.?

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;


}


1 comment:

  1. In C and C++ language, string is useful to display the the connected character or group of character in the statement like the printf. It is used to display something on the terminal. If you more information about string, post here.

    acai berry

    ReplyDelete