source: trunk/libs/newlib/src/newlib/libc/stdlib/itoa.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<<itoa>>---integer to string
4
5INDEX
6        itoa
7
8SYNOPSIS
9        #include <stdlib.h>
10        char *itoa(int <[value]>, char *<[str]>, int <[base]>);
11        char *__itoa(int <[value]>, char *<[str]>, int <[base]>);
12
13DESCRIPTION
14<<itoa>> converts the integer <[value]> to a null-terminated string
15using the specified base, which must be between 2 and 36, inclusive.
16If <[base]> is 10, <[value]> is treated as signed and the string will be
17prefixed with '-' if negative. For all other bases, <[value]> is treated as
18unsigned. <[str]> should be an array long enough to contain the converted
19value, which in the worst case is sizeof(int)*8+1 bytes.
20
21RETURNS
22A pointer to the string, <[str]>, or NULL if <[base]> is invalid.
23
24PORTABILITY
25<<itoa>> is non-ANSI.
26
27No supporting OS subroutine calls are required.
28*/
29
30#include <stdlib.h>
31
32char *
33__itoa (int value,
34        char *str,
35        int base)
36{
37  unsigned uvalue;
38  int i = 0;
39 
40  /* Check base is supported. */
41  if ((base < 2) || (base > 36))
42    { 
43      str[0] = '\0';
44      return NULL;
45    } 
46   
47  /* Negative numbers are only supported for decimal.
48   * Cast to unsigned to avoid overflow for maximum negative value.  */ 
49  if ((base == 10) && (value < 0))
50    {             
51      str[i++] = '-';
52      uvalue = (unsigned)-value;
53    }
54  else
55    uvalue = (unsigned)value;
56 
57  __utoa (uvalue, &str[i], base);
58  return str;
59}
60
61char * 
62itoa (int value,
63        char *str,
64        int base)
65{
66  return __itoa (value, str, base);
67}
Note: See TracBrowser for help on using the repository browser.