integer to string function (itoa())

carroll at m.cs.uiuc.edu carroll at m.cs.uiuc.edu
Sun Jul 1 03:33:00 AEST 1990


/* Written 11:15 am  Jun 29, 1990 by brad at SSD.CSD.HARRIS.COM in m.cs.uiuc.edu:comp.lang.c */
>In article <22888 at boulder.Colorado.EDU> baileyc at tramp.Colorado.EDU (BAILEY CHRISTOPHER R) writes:
>
>>Help, I need an itoa() function for use with my Sun compiler.
>
>Perhaps I missed something but ...  Is there some reason why:
>
>  int i = 10; char a[3];
>  sprintf( a, "%d", i );
>
>is unnacceptable for your purposes?
I can't speak for Mr. Bailey, but I have such code because sprintf() was
inadequate. In Epoch there is a need to convert longs to several different
bases (i.e., 2,8,10,16) in a general way. Rolling your own itoa() is (IMHO)
actually easier than kludging with special cases and sprintf(), not to
mention being more efficient (sprintf() does so ugly things in many cases
to work). I don't understand why this _isn't_ in the standard library, since
the inverse is available. One thing that's pissed me off in the past is that
I can read binary easily (using atol()), but I can't write it with equal
facility.



More information about the Comp.lang.c mailing list