diff options
author | Jon Santmyer <jon@jonsantmyer.com> | 2024-05-22 13:00:41 -0400 |
---|---|---|
committer | Jon Santmyer <jon@jonsantmyer.com> | 2024-05-22 13:00:41 -0400 |
commit | ace65b453151845bc361f21f3e5b651c35f9f126 (patch) | |
tree | 262ebd29b0ca1d8584f0b6f1efa7a00d9f4f3e43 /klib/ltostr.c | |
parent | f004c1ade8d617a82cea2fe249434cccb47a2358 (diff) | |
download | jove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.tar.gz jove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.tar.bz2 jove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.zip |
Diffstat (limited to 'klib/ltostr.c')
-rw-r--r-- | klib/ltostr.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/klib/ltostr.c b/klib/ltostr.c new file mode 100644 index 0000000..e28be31 --- /dev/null +++ b/klib/ltostr.c @@ -0,0 +1,26 @@ +#include "format.h" + +size_t +ltostr(char *s, size_t limit, unsigned long l, bool sgn, int radix) +{ + size_t si = 0; + size_t digits = 0; + if((long)l < 0 && sgn) { + l = -((long)l); + s[0] = '-'; + } + for(unsigned long lv = l; lv != 0; lv /= radix) + digits++; + digits = digits > limit ? limit : digits; + + if(digits-- == 0) + s[si++] = '0'; + for(unsigned long lv = l; lv != 0; lv /= radix) + { + if(si >= limit) return si; + int digit = lv % radix; + s[(digits - si)] = (digit >= 10 ? (digit + 'a' - 10) : digit + '0'); + si++; + } + return si; +} |