From a0b8c61b09200aa3f9e27878cb866648a7d26502 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 23 Jul 2020 20:13:25 +0200 Subject: Added formatted print function --- src/lib/conv.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/lib/conv.c') diff --git a/src/lib/conv.c b/src/lib/conv.c index 89d1786..3bde4ec 100644 --- a/src/lib/conv.c +++ b/src/lib/conv.c @@ -114,3 +114,31 @@ char *itoa(int n) strinv(ret); return ret; } + +char *conv_base(int value, char *result, int base, int is_signed) +{ + if (base < 2 || base > 36) { + *result = '\0'; + return result; + } + + char *ptr = result, *ptr1 = result, tmp_char; + int tmp_value; + + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" + [35 + (tmp_value - value * base)]; + } while (value); + + if (is_signed && tmp_value < 0) + *ptr++ = '-'; + *ptr-- = '\0'; + while (ptr1 < ptr) { + tmp_char = *ptr; + *ptr-- = *ptr1; + *ptr1++ = tmp_char; + } + return result; +} -- cgit v1.2.3