aboutsummaryrefslogtreecommitdiff
path: root/lib/string/itoa.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string/itoa.c')
-rw-r--r--lib/string/itoa.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/string/itoa.c b/lib/string/itoa.c
index 5eefb37..3708be2 100644
--- a/lib/string/itoa.c
+++ b/lib/string/itoa.c
@@ -1,13 +1,30 @@
-#include "string.h"
+#include <stdbool.h>
+#include <string.h>
+
+static const char *numbers = "0123456789abcdef";
+
+char *
+itoa(char *p, int x, int base)
+{
+ const bool is_negative = (x < 0);
+ if (is_negative) x = -x;
+
+ p = utoa(p, x, base);
+
+ if (is_negative) *--p = '-';
+ return p;
+}
char *
-itoa(char *p, unsigned x)
+utoa(char *p, unsigned x, int base)
{
- p += 3 * sizeof(int);
- *--p = 0;
+ p += 3 * sizeof(unsigned);
+ *--p = '\0';
+
do {
- *--p = '0' + x % 10;
- x /= 10;
+ *--p = numbers[x % base];
+ x /= base;
} while (x);
+
return p;
}