Index: newlib/libc/include/stdlib.h =================================================================== RCS file: /cvs/src/src/newlib/libc/include/stdlib.h,v retrieving revision 1.52 diff -u -p -r1.52 stdlib.h --- newlib/libc/include/stdlib.h 10 Oct 2014 14:43:19 -0000 1.52 +++ newlib/libc/include/stdlib.h 8 Dec 2014 18:07:41 -0000 @@ -175,6 +175,8 @@ char * _EXFUN(ecvtbuf,(double, int, int* char * _EXFUN(fcvtbuf,(double, int, int*, int*, char *)); char * _EXFUN(ecvtf,(float,int,int *,int *)); char * _EXFUN(dtoa,(double, int, int, int *, int*, char**)); +char * _EXFUN(itoa,(int, char *, int)); +char * _EXFUN(utoa,(unsigned, char *, int)); int _EXFUN(rand_r,(unsigned *__seed)); double _EXFUN(drand48,(_VOID)); Index: newlib/libc/stdlib/Makefile.am =================================================================== RCS file: /cvs/src/src/newlib/libc/stdlib/Makefile.am,v retrieving revision 1.31 diff -u -p -r1.31 Makefile.am --- newlib/libc/stdlib/Makefile.am 29 May 2013 12:37:59 -0000 1.31 +++ newlib/libc/stdlib/Makefile.am 8 Dec 2014 18:07:41 -0000 @@ -31,6 +31,7 @@ GENERAL_SOURCES = \ gdtoa-hexnan.c \ getenv.c \ getenv_r.c \ + itoa.c \ labs.c \ ldiv.c \ ldtoa.c \ @@ -52,6 +53,7 @@ GENERAL_SOURCES = \ strtod.c \ strtol.c \ strtoul.c \ + utoa.c \ wcstod.c \ wcstol.c \ wcstoul.c \ @@ -257,6 +259,7 @@ CHEWOUT_FILES= \ envlock.def \ exit.def \ getenv.def \ + itoa.def \ labs.def \ ldiv.def \ llabs.def \ @@ -277,6 +280,7 @@ CHEWOUT_FILES= \ strtoll.def \ strtoul.def \ strtoull.def \ + utoa.def \ wcsnrtombs.def \ wcstod.def \ wcstol.def \ Index: newlib/libc/stdlib/itoa.c =================================================================== RCS file: newlib/libc/stdlib/itoa.c diff -N newlib/libc/stdlib/itoa.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ newlib/libc/stdlib/itoa.c 8 Dec 2014 18:07:41 -0000 @@ -0,0 +1,58 @@ +/* +FUNCTION +<>---integer to string + +INDEX + itoa + +ANSI_SYNOPSIS + #include + char *itoa(int <[value]>, char *<[str]>, int <[base]>); + +DESCRIPTION +<> converts the integer [] to a null-terminated string +using the specified base, which must be between 2 and 36, inclusive. +If <[base]> is 10, <[value]> is treated as signed and the string will be +prefixed with '-' if negative. For all other bases, <[value]> is treated as +unsigned. <[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 (itoa, (value, str, base), + int value _AND + char *str _AND + int base) +{ + unsigned uvalue; + + /* Check base is supported. */ + if ((base < 2) || (base > 36)) + { + str[0] = '\0'; + return NULL; + } + + /* Convert to string. Digits are in reverse order. */ + /* Negative numbers are only supported for decimal. + * Cast to unsigned to avoid overflow for maximum negative value. */ + if ((base == 10) && (value < 0)) + { + *str++ = '-'; + uvalue = (unsigned)-value; + } + else + uvalue = (unsigned)value; + + return utoa (uvalue, str, base); +} Index: newlib/libc/stdlib/utoa.c =================================================================== RCS file: newlib/libc/stdlib/utoa.c diff -N newlib/libc/stdlib/utoa.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ newlib/libc/stdlib/utoa.c 8 Dec 2014 18:07:41 -0000 @@ -0,0 +1,66 @@ +/* +FUNCTION +<>---unsigned integer to string + +INDEX + utoa + +ANSI_SYNOPSIS + #include + 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; +}