GNU gclib

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Post 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;
}
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post 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
Post Reply