Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!shelby!csli!poser From: poser@csli.Stanford.EDU (Bill Poser) Newsgroups: comp.lang.c Subject: Re: integer to string conversion Keywords: code Message-ID: <9814@csli.Stanford.EDU> Date: 24 Jul 89 10:14:09 GMT References: <3020@ohstpy.mps.ohio-state.edu> <9813@csli.Stanford.EDU> <18702@mimsy.UUCP> Sender: poser@csli.Stanford.EDU (Bill Poser) Reply-To: poser@csli.stanford.edu (Bill Poser) Organization: Center for the Study of Language and Information, Stanford U. Lines: 18 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?