integer to string conversion
Bill Poser
poser at csli.Stanford.EDU
Mon Jul 24 20:14:09 AEST 1989
It is correct that the routine I posted doesn't do the right thing on the
most negative integer. That requires a special case, which I left out.
Chris Torek is correct that a binary search will speed it up even more,
but as he says that means knowing the range of integers on the machine,
which reduces portability. The simplest and most portable way (across
environments where sprintf is available) to do this
is to use sprintf, the fastest and least portable is to write an
assembly language routine that takes advantage of the machine's
instruction set and knowledge of the size of integers. The routine
I provided is the middle ground - it gives a speed advantage over
sprintf (at least on certain common processors) but remains pretty portable.
I think Chris is being cranky about the need for the routine.
I began with the most straightforward solution (sprintf), gave a routine
that shows how the conversion is done, and was explicit about the
magnitude of its speed advantage and how it might vary. What more can you
ask for?
More information about the Comp.lang.c
mailing list