source: trunk/libs/newlib/src/newlib/libc/stdlib/utoa.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.4 KB
Line 
1/*
2FUNCTION
3<<utoa>>---unsigned integer to string
4
5INDEX
6        utoa
7
8SYNOPSIS
9        #include <stdlib.h>
10        char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
11        char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>);
12
13DESCRIPTION
14<<utoa>> converts the unsigned integer [<value>] to a null-terminated string
15using the specified base, which must be between 2 and 36, inclusive.
16<[str]> should be an array long enough to contain the converted
17value, which in the worst case is sizeof(int)*8+1 bytes.
18
19RETURNS
20A pointer to the string, <[str]>, or NULL if <[base]> is invalid.
21
22PORTABILITY
23<<utoa>> is non-ANSI.
24
25No supporting OS subroutine calls are required.
26*/
27
28#include <stdlib.h>
29
30char *
31__utoa (unsigned value,
32        char *str,
33        int base)
34{
35  const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
36  int i, j;
37  unsigned remainder;
38  char c;
39 
40  /* Check base is supported. */
41  if ((base < 2) || (base > 36))
42    { 
43      str[0] = '\0';
44      return NULL;
45    } 
46   
47  /* Convert to string. Digits are in reverse order.  */
48  i = 0;
49  do 
50    {
51      remainder = value % base;
52      str[i++] = digits[remainder];
53      value = value / base;
54    } while (value != 0); 
55  str[i] = '\0'; 
56 
57  /* Reverse string.  */
58  for (j = 0, i--; j < i; j++, i--)
59    {
60      c = str[j];
61      str[j] = str[i];
62      str[i] = c; 
63    }       
64 
65  return str;
66}
67
68char * 
69utoa (unsigned value,
70        char *str,
71        int base)
72{
73  return __utoa (value, str, base);
74}
Note: See TracBrowser for help on using the repository browser.