Thursday, July 30, 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.

Use C programing language to write a function htoi(s), which converts a string of hexadecimal digits to int.?
/* only small letters are taken care in this program


you can modify it to take care of capital letters too





*/


// Enjoy ...!!!!!!!!!!!!





#include%26lt;stdio.h%26gt;


main()


{


char s[10];


char *p,c;


int i, sum = 0;


printf("enter hexadecimal number start with 0x\n");





scanf("%s",s);


p=s;





printf("\nYou entered : %s\n",p);


while ( *p != '\0' )


{


c = *p;


if(c == '0' %26amp;%26amp; (*(p+1)) == 'x') p=p+2;


c = *p++;


switch (c)


{


case 'a' : i=10; break;


case 'b' : i=11; break;


case 'c' : i=12; break;


case 'd' : i=13; break;


case 'e' : i=14; break;


case 'f' : i=15; break;


default :


i = (int)(c) - (int)('0') ;


}


sum = 16*sum + i;


}


printf("%s = %d\n",s,sum);


}
Reply:i would do it basically the same way u wud do it if it were decimal





firstly, check for the 0x





then find out the length





then start at rightmost, and work ur way to left, like





110 in decimal


0*1 + 1*10 + 1*100 = 110





do same way for hexadecimal except multiply digit by 16 (to convert characters to their equivalent numbers in hexadecimal, ul probly want to look at an ascii table instead of using a bunch of if thens)
Reply:well, start at the end of the string, add the value of the last charector to an accumulating integer variable. then check next to last, multiply it's value by 16, then add it to the accumulating int. then check the next(moving from last to first), multiply it by 256, then add it to the accumulator





the thing to remember is to multiply the value by the next power of 16 for each charector you are adding the value for





let's say you get 0xA8F9


you want 43257





first add 9 to the accumulating var (if it was a letter, it would be 10-15 for a-f





the next char is 'f' f is 15. so multiply 15 by 16^1 or 16. you get 240, add it to the accumulator


249





the next char is 8, so multiply 8 by 16^2 or 256. you get 2048


add it to the accumulator, now at 249


2297





the next char is 'A' it is 10, so multiply 10 by 16^3.


that is 40960 add it to the accumulator


43257





when you se the x charector, or have checked the whole string, then return your value, your done





I just picked the number out of air, this method works for converting all bases, just change the 16 to the base you have. in oct(8 base) take the values by 8^x. for binary, take it 2^x in decimal, scientific notation is written by n.nnn(10^x) showing that this works for 10 base as well.





as for the code, you have to write it. get something compiled, then if it's got bugs, post the code, and you could get some help debuging.
Reply:100 PRINT "Hello World"


200 GOTO 100


No comments:

Post a Comment