[Main]
Title=How can I write an efficient number to string routine?

[Top]
<TABLE CELLPADDING="4"><TR><TD VALIGN="TOP"><B>Q:</B></TD><TD>
I am just curious how I can write my own and efficient IntToStr routine...
</TD></TR><TR><TD VALIGN="TOP"><B>A:</B></TD><TD>
Process the number starting from the end (i.e. from the least
significant digit instead from the most significant digit), like in the
following example, which is probably the most optimal code:
<PRE>char *IntToStr (unsigned long an_integer)
{
  static char result [] = "          \0";    // <I>10 spaces and \0</I>
  char *ptr = result + 10;
  while (an_integer)
    {
      *ptr-- = an_integer % 10 + '0';
      an_integer/=10;
    }
  return ptr;
}
</PRE>
Note that <CODE>'static'</CODE> before char in the first line is essential: without it, the
variable <CODE>'result'</CODE> will be allocated of the stack, so it will not live
too long after this function returns. Returning any pointers which points to
structures allocated on the stack is extremely dangerous (it is not only dangerous; it is
almost completely nonsense, except if you performs some really nasty and bizzare hacks).
The another solution (but less elegant) is to make <CODE>'result'</CODE> global (i.e. to
define it out of the function).
</TD></TR></TABLE>
