blob: 4ae509ace7cdc999e0abcbab51a86887fda87c70 (
plain)
1 #include "common.h"
2
3 /**
4 * The complimentary function to the posix atoi. It turns an integer into a
5 * string.
6 *
7 * @param i Integer to be converted to string
8 * @param out Output char array. Be sure it has enough room for the output
9 * string.
10 *
11 * @return int The character count of the output char array
12 */
13 int itoa(int i, char* out) {
14 // To keep track of where we are in the array
15 int n = 0;
16 while(i > 9) {
17 printf(" %d\n", i);
18 // This is some crazy simple math here
19 out[n] = (i%10) + 48;
20 // It's an int, so the last number will be stripped
21 i = i/10;
22 n++;
23 }
24 // Have to do this one more time to ensure we get the last number on
25 // zero-ended ints
26 out[n] = (i%10) + 48;
27 // Account for the last char
28 n++;
29 // Annnd the null byte
30 out[n] = '\0';
31 n++;
32 return n;
33 }
|