diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-04-01 21:46:28 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-08-12 10:13:59 +0300 |
commit | 9b777b088facde26c33df6c746b948caff725602 (patch) | |
tree | 3e88f6808e80a233f3e78640d1f2545489665376 /lib/string | |
parent | lidt (diff) | |
download | kernel-9b777b088facde26c33df6c746b948caff725602.tar.xz |
printf: add %d, %u and %x
Diffstat (limited to 'lib/string')
-rw-r--r-- | lib/string/itoa.c | 29 |
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; } |