?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*-------------------------------------------------------------------------
  2.    _ltoa.c - integer to string conversion
  3.  
  4.    Copyright (c) 1999, Bela Torok, bela.torok@kssg.ch
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. /*-------------------------------------------------------------------------
  30.  usage:
  31.  
  32.  _ultoa(unsigned long value, char* string, int radix)
  33.  _ltoa(long value, char* string, int radix)
  34.  
  35.  value  ->  Number to be converted
  36.  string ->  Result
  37.  radix  ->  Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
  38. ---------------------------------------------------------------------------*/
  39.  
  40. /* "11110000111100001111000011110000" base 2 */
  41. /* "37777777777" base 8 */
  42. /* "4294967295" base 10 */
  43. #define NUMBER_OF_DIGITS 32     /* eventually adapt if base 2 not needed */
  44.  
  45. #if NUMBER_OF_DIGITS < 32
  46. # warning _ltoa() and _ultoa() are not save for radix 2
  47. #endif
  48.  
  49. #if defined (__SDCC_mcs51) && defined (__SDCC_MODEL_SMALL) && !defined (__SDCC_STACK_AUTO)
  50. # define MEMSPACE_BUFFER __idata        /* eventually __pdata or __xdata */
  51. # pragma nogcse
  52. #else
  53. # define MEMSPACE_BUFFER
  54. #endif
  55.  
  56. void _ultoa(unsigned long value, char* string, unsigned char radix)
  57. {
  58.   char MEMSPACE_BUFFER buffer[NUMBER_OF_DIGITS];  /* no space for '\0' */
  59.   unsigned char index = NUMBER_OF_DIGITS;
  60.  
  61.   do {
  62.     unsigned char c = '0' + (value % radix);
  63.     if (c > (unsigned char)'9')
  64.        c += 'A' - '9' - 1;
  65.     buffer[--index] = c;
  66.     value /= radix;
  67.   } while (value);
  68.  
  69.   do {
  70.     *string++ = buffer[index];
  71.   } while ( ++index != NUMBER_OF_DIGITS );
  72.  
  73.   *string = 0;  /* string terminator */
  74. }
  75.  
  76. void _ltoa(long value, char* string, unsigned char radix)
  77. {
  78.   if (value < 0 && radix == 10) {
  79.     *string++ = '-';
  80.     value = -value;
  81.   }
  82.   _ultoa(value, string, radix);
  83. }
  84.  
  85.