Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / utoa.c
blob5ef304ce49bfa826c206e37aa5c874f6cedb1de5
1 /*
2 FUNCTION
3 <<utoa>>---unsigned integer to string
5 INDEX
6 utoa
8 SYNOPSIS
9 #include <stdlib.h>
10 char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
11 char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
13 DESCRIPTION
14 <<utoa>> converts the unsigned integer [<value>] to a null-terminated string
15 using the specified base, which must be between 2 and 36, inclusive.
16 <[str]> should be an array long enough to contain the converted
17 value, which in the worst case is sizeof(int)*8+1 bytes.
19 RETURNS
20 A pointer to the string, <[str]>, or NULL if <[base]> is invalid.
22 PORTABILITY
23 <<utoa>> is non-ANSI.
25 No supporting OS subroutine calls are required.
28 #include <stdlib.h>
30 char *
31 __utoa (unsigned value,
32 char *str,
33 int base)
35 const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
36 int i, j;
37 unsigned remainder;
38 char c;
40 /* Check base is supported. */
41 if ((base < 2) || (base > 36))
43 str[0] = '\0';
44 return NULL;
47 /* Convert to string. Digits are in reverse order. */
48 i = 0;
49 do
51 remainder = value % base;
52 str[i++] = digits[remainder];
53 value = value / base;
54 } while (value != 0);
55 str[i] = '\0';
57 /* Reverse string. */
58 for (j = 0, i--; j < i; j++, i--)
60 c = str[j];
61 str[j] = str[i];
62 str[i] = c;
65 return str;
68 char *
69 utoa (unsigned value,
70 char *str,
71 int base)
73 return __utoa (value, str, base);