Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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