* Removed svn:executable property from non-executable files
[fw/sdcc] / device / lib / _ltoa.c
1 /*-------------------------------------------------------------------------
2  integer to string conversion
3
4  Written by:   Bela Torok, 1999
5                bela.torok@kssg.ch
6  usage:
7
8  _ultoa(unsigned long value, char* string, int radix)
9  _ltoa(long value, char* string, int radix)
10
11  value  ->  Number to be converted
12  string ->  Result
13  radix  ->  Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
14 ---------------------------------------------------------------------------*/
15
16 /* "11110000111100001111000011110000" base 2 */
17 /* "37777777777" base 8 */
18 /* "4294967295" base 10 */
19 #define NUMBER_OF_DIGITS 32     /* eventually adapt if base 2 not needed */
20
21 #if NUMBER_OF_DIGITS < 32
22 # warning _ltoa() and _ultoa() are not save for radix 2
23 #endif
24
25 #if defined (SDCC_mcs51) && defined (SDCC_MODEL_SMALL) && !defined (SDCC_STACK_AUTO)
26 # define MEMSPACE_BUFFER __idata        /* eventually __pdata or __xdata */
27 # pragma nogcse
28 #else
29 # define MEMSPACE_BUFFER 
30 #endif
31
32 void _ultoa(unsigned long value, char* string, unsigned char radix)
33 {
34   char MEMSPACE_BUFFER buffer[NUMBER_OF_DIGITS];  /* no space for '\0' */
35   unsigned char index = NUMBER_OF_DIGITS;
36
37   do {
38     unsigned char c = '0' + (value % radix);
39     if (c > '9') 
40        c += 'A' - '9' - 1;
41     buffer[--index] = c;
42     value /= radix;
43   } while (value);
44
45   do {
46     *string++ = buffer[index];
47   } while ( ++index != NUMBER_OF_DIGITS );
48
49   *string = 0;  /* string terminator */
50 }
51
52 void _ltoa(long value, char* string, unsigned char radix)
53 {
54   if (value < 0 && radix == 10) {
55     *string++ = '-';
56     value = -value;
57   }
58   _ultoa(value, string, radix);
59 }