aboutsummaryrefslogtreecommitdiff
path: root/src/lib/conv.c
diff options
context:
space:
mode:
authorMarvin Borner2020-07-23 20:13:25 +0200
committerMarvin Borner2020-07-23 20:13:25 +0200
commita0b8c61b09200aa3f9e27878cb866648a7d26502 (patch)
tree31de26c3c491d3fee719a1d9d5aad2b2ce879e2a /src/lib/conv.c
parent99e183a2f569729d722d83503cb851d6198fc1fe (diff)
Added formatted print function
Diffstat (limited to 'src/lib/conv.c')
-rw-r--r--src/lib/conv.c28
1 files changed, 28 insertions, 0 deletions
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;
+}