Home arrow Support arrow Forums

Luminary Micro Forums

mdst

Senior Boarder

2008/11/14 22:29

itoa.c (number to char conversion)

The itoa() function is either not implemented or I can't find it (Using Code_Red). Using sprinf leads to code bloat. So the only way I have been able to covert numbers to chars, ie: for OLED display functions, is to implement the itoa() function from scratch. Here's the code if anyone is interested.


Code:

  #include <string.h> /* reverse:  reverse string s in place */ void reverse(char s[]) {     int cij;     for (0strlen(s)-1i<ji++, j--) {         s[i];         s[i] = s[j];         s[j] = c;     } } /* itoa:  convert n to characters in s */ void itoa(int nchar s[]) {     int isign;     if ((sign n) < 0)  /* record sign */         = -n;          /* make n positive */     0;     do {       /* generate digits in reverse order */         s[i++] = 10 '0';   /* get next digit */     } while ((/= 10) > 0);     /* delete it */     if (sign 0)         s[i++] = '-';     s[i] = '\0';     reverse(s); } 



For more info see: http://en.wikipedia.org/wiki/Itoa

Post edited by: mdst, at: 2008/11/14 22:30

Post edited by: mdst, at: 2008/11/14 22:32

Post edited by: mdst, at: 2008/11/14 22:33

login or register to reply

noti

Fresh Boarder

2008/11/26 03:49

Re:itoa.c (number to char conversion)

Thanks for your suggestion

I programmed a similar function, but startet filling the string from its end to get rid of the reverse order (very simple for fixed string lengths, but with leading zeroes).

If you dont like leading zeroes its also possible to calculate the string size before filling it (eg number of /10) so you dont have to use "strlen".

Post edited by: noti, at: 2008/11/26 03:50

login or register to reply