Page 2 of 2

Posted: Thu Aug 31, 2006 3:45 pm
by bubach
Here's some public domain code I found:

Code: Select all

/* ITOA
   Example:
      char str[6];
      itoa(str, 568);
*/
char *itoa(char *str, int num) 
{
   int k;
   char c, flag, *ostr;

   if (num < 0) {
      num = -num;
      *str++ = '-';
   }
   k = 10000;
   ostr = str;
   flag = 0;
   while (k) {
      c = num / k;
      if (c || k == 1 || flag) {
         num %= k;
         c += '0';
         *str++ = c;
         flag = 1;
      }
      k /= 10;
   }
   *str = '\0';
   return ostr;
}

Posted: Sat Sep 02, 2006 9:22 am
by kenneth_phough
Thank you very much for the source code! :D
Know I finally understand how to convert integers to ASCII. The source code I was originally looking at used something called an index (an integer) which was part of the argument of the function, and I couldn't figure out what that was doing to the integer to convert it to ascii.
But now I understand! <- thanks to you 8) [bubach]

Cheers,
Kenneth