diff options
Diffstat (limited to 'src/lib/conv.c')
-rw-r--r-- | src/lib/conv.c | 28 |
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; +} |