1 /*
2 FUNCTION
3 <<utoa>>---unsigned integer to string
4 
5 INDEX
6 	utoa
7 
8 ANSI_SYNOPSIS
9 	#include <stdlib.h>
10 	char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
11 	char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
12 
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.
18 
19 RETURNS
20 A pointer to the string, <[str]>, or NULL if <[base]> is invalid.
21 
22 PORTABILITY
23 <<utoa>> is non-ANSI.
24 
25 No supporting OS subroutine calls are required.
26 */
27 
28 #include <stdlib.h>
29 
30 char *
31 _DEFUN (__utoa, (value, str, base),
32         unsigned value _AND
33         char *str _AND
34         int base)
35 {
36   const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
37   int i, j;
38   unsigned remainder;
39   char c;
40 
41   /* Check base is supported. */
42   if ((base < 2) || (base > 36))
43     {
44       str[0] = '\0';
45       return NULL;
46     }
47 
48   /* Convert to string. Digits are in reverse order.  */
49   i = 0;
50   do
51     {
52       remainder = value % base;
53       str[i++] = digits[remainder];
54       value = value / base;
55     } while (value != 0);
56   str[i] = '\0';
57 
58   /* Reverse string.  */
59   for (j = 0, i--; j < i; j++, i--)
60     {
61       c = str[j];
62       str[j] = str[i];
63       str[i] = c;
64     }
65 
66   return str;
67 }
68 
69 char *
70 _DEFUN (utoa, (value, str, base),
71         unsigned value _AND
72         char *str _AND
73         int base)
74 {
75   return __utoa (value, str, base);
76 }
77