From 32c96ddd14acadc82e7fccf110367ec3c8320c85 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Tue, 16 Dec 2014 10:55:17 +0000 Subject: * libc/include/stdlib.h (__itoa): Declare prototype. (__utoa): Ditto. (itoa): Ditto, non-strict-ANSI only. (utoa): Ditto. * libc/stdlib/Makefile.am: Add itoa.c and utoa.c. * libc/stdlib/Makefile.in: Regenerate. * libc/stdlib/itoa.c: New file. * libc/stdlib/utoa.c: New file. --- newlib/libc/stdlib/utoa.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 newlib/libc/stdlib/utoa.c (limited to 'newlib/libc/stdlib/utoa.c') diff --git a/newlib/libc/stdlib/utoa.c b/newlib/libc/stdlib/utoa.c new file mode 100644 index 000000000..7738c2321 --- /dev/null +++ b/newlib/libc/stdlib/utoa.c @@ -0,0 +1,76 @@ +/* +FUNCTION +<>---unsigned integer to string + +INDEX + utoa + +ANSI_SYNOPSIS + #include + char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>); + char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>); + +DESCRIPTION +<> converts the unsigned integer [] to a null-terminated string +using the specified base, which must be between 2 and 36, inclusive. +<[str]> should be an array long enough to contain the converted +value, which in the worst case is sizeof(int)*8+1 bytes. + +RETURNS +A pointer to the string, <[str]>, or NULL if <[base]> is invalid. + +PORTABILITY +<> is non-ANSI. + +No supporting OS subroutine calls are required. +*/ + +#include + +char * +_DEFUN (__utoa, (value, str, base), + unsigned value _AND + char *str _AND + int base) +{ + const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + int i, j; + unsigned remainder; + char c; + + /* Check base is supported. */ + if ((base < 2) || (base > 36)) + { + str[0] = '\0'; + return NULL; + } + + /* Convert to string. Digits are in reverse order. */ + i = 0; + do + { + remainder = value % base; + str[i++] = digits[remainder]; + value = value / base; + } while (value != 0); + str[i] = '\0'; + + /* Reverse string. */ + for (j = 0, i--; j < i; j++, i--) + { + c = str[j]; + str[j] = str[i]; + str[i] = c; + } + + return str; +} + +char * +_DEFUN (utoa, (value, str, base), + unsigned value _AND + char *str _AND + int base) +{ + return __utoa (value, str, base); +} -- cgit v1.2.3