summaryrefslogtreecommitdiff
path: root/libc/misc/internals/ulltostr.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2000-12-20 22:52:58 +0000
committerEric Andersen <andersen@codepoet.org>2000-12-20 22:52:58 +0000
commitc6218dbae579de0cd20f5a7f1e9877673e28225d (patch)
tree0fc8aaf54189b8ef6a2d130c12539814e0a724ee /libc/misc/internals/ulltostr.c
parent97112ff6f4f2a1dcd4c7f8a7512e0a4a02a2a332 (diff)
A number of updates from Manuel Novoa III. Things look good...
Diffstat (limited to 'libc/misc/internals/ulltostr.c')
-rw-r--r--libc/misc/internals/ulltostr.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/libc/misc/internals/ulltostr.c b/libc/misc/internals/ulltostr.c
new file mode 100644
index 000000000..50246d3bc
--- /dev/null
+++ b/libc/misc/internals/ulltostr.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2000 Manuel Novoa III
+ *
+ * Note: buf is a pointer to the END of the buffer passed.
+ * Call like this:
+ * char buf[SIZE], *p;
+ * p = __ulltostr(buf + sizeof(buf) - 1, ...)
+ *
+ * For long longs of 64 bits, appropriate buffer sizes are:
+ * base = 2 65 = 64 digits + 1 nul
+ * base = 10 20 = 19 digits + 1 nul
+ * base = 16 17 = 16 hex digits + 1 nul
+ */
+
+char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase)
+{
+ int digit;
+
+ if ((base < 2) || (base > 36)) {
+ return 0;
+ }
+
+ *buf = '\0';
+
+ do {
+ digit = uval % base;
+ uval /= base;
+
+ /* note: slightly slower but generates less code */
+ *--buf = '0' + digit;
+ if (digit > 9) {
+ *buf = (uppercase ? 'A' : 'a') + digit - 10;
+ }
+ } while (uval);
+
+ return buf;
+}