Monday, July 27, 2009

Convert string to integer in C, without using itoa function?

Basically, I want to know what's "behind" itoa... A site which explains/expands functions in C would be useful, too.

Convert string to integer in C, without using itoa function?
void itoa (char *buf, int base, int d)


{


char *p = buf;


char *p1, *p2;


unsigned long ud = d;


int divisor = 10;





/* If %d is specified and D is minus, put `-' in the head. */


if (base == 'd' %26amp;%26amp; d %26lt; 0)


{


*p++ = '-';


buf++;


ud = -d;


}


else if (base == 'x')


divisor = 16;





/* Divide US by divisor until UD == 0. */


do


{


int remainder = ud % divisor;





*p++ = (remainder %26lt; 10) ? remainder + '0' : remainder + 'a' - 10;


} while (ud /= divisor);








/* hex values */


if(base == 'x')


{


*p++ = 'x';


*p++ = '0';


}





/* Reverse it */


*p = 0;


p1 = buf;


p2 = p - 1;


while (p1 %26lt; p2)


{


char tmp = *p1;


*p1 = *p2;


*p2 = tmp;


p1++;


p2--;


}


}
Reply:I don't know what is behind itoa... however, I did oncewrite a c function to convert integer to individual asci codes, then display it using my own little text system for GBA using HAM





here is the smaller one, does 4 digits from an unsigned 16 bit number...





void Draw_4dd(u16 num,u8 nx,u8 ny)


{


if(num%26gt;9999)num=9999;





u16 Th=0;


u16 Hu=0;


u16 Te=0;


u16 On=0;





Th= (num/1000);


Hu= ((num-(Th*1000))/100);


Te= ((num-(Th*1000)-(Hu*100))/10);


On= (num-(Th*1000)-(Hu*100)-(Te*10));





Th=Th+48;


Hu=Hu+48;


Te=Te+48;


On=On+48;





if(num%26gt;999)


Draw_Text(Th,nx,ny);


if(num%26gt;99)


Draw_Text(Hu,nx+1,ny);


if(num%26gt;9)


Draw_Text(Te,nx+2,ny);


Draw_Text(On,nx+3,ny);


}





u16 and u8 are unsigned 16 and 8 bit vars. I break the number down into ten's place values, then add the code for 1(48) to them(giving me the asci codes for the numbers), then pass it to my single charector text plotter function Draw_Text I was going to write a faster version of this, but never got to it. the only thing that makes this work is the fact that integer values don't keep a decimal value. when you divide 1234 by 1000, you still get 1 as a result. multiply back by 1000, you get 1000. the fact that the computer is in 2 base is irrelevant, because i use a seperate var for each 10 place value.





converting string to integer would be the same thing backwards





what I would do is treat the string as an array, walking through each char, from the end to the front, multiplying each new number by the next ten power, then adding it to an accumulating integer variable. You would also have to look for indicators, like spaces, commas, dollar signs, and bad data, like letters...





hope this helps a little.
Reply:Ok... 124 = 100 + 20 + 4. In other words, 124 = 1*100 + 2*10 + 4*1. Get the idea?





The actual source code should have a more efficient algorithm, probably with binary math, logical shifts, shortcuts, etc. It could even be written in assembler. Many compilers do include the source code of their libraries.

wildflower

No comments:

Post a Comment