summaryrefslogtreecommitdiff
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..4ae509a
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,34 @@
+#include "common.h"
+
+/**
+ * The complimentary function to the posix atoi. It turns an integer into a
+ * string.
+ *
+ * @param i Integer to be converted to string
+ * @param out Output char array. Be sure it has enough room for the output
+ * string.
+ *
+ * @return int The character count of the output char array
+ */
+int itoa(int i, char* out) {
+ // To keep track of where we are in the array
+ int n = 0;
+ while(i > 9) {
+ printf(" %d\n", i);
+ // This is some crazy simple math here
+ out[n] = (i%10) + 48;
+ // It's an int, so the last number will be stripped
+ i = i/10;
+ n++;
+ }
+ // Have to do this one more time to ensure we get the last number on
+ // zero-ended ints
+ out[n] = (i%10) + 48;
+ // Account for the last char
+ n++;
+ // Annnd the null byte
+ out[n] = '\0';
+ n++;
+ return n;
+}
+

Generated by cgit