1 /*-------------------------------------------------------------------------
2 _ltoa.c - integer to string conversion
4 Copyright (c) 1999, Bela Torok, bela.torok@kssg.ch
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
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.
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,
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 -------------------------------------------------------------------------*/
29 /*-------------------------------------------------------------------------
32 __ultoa(unsigned long value, char* string, int radix)
33 __ltoa(long value, char* string, int radix)
35 value -> Number to be converted
37 radix -> Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
38 ---------------------------------------------------------------------------*/
42 /* "11110000111100001111000011110000" base 2 */
43 /* "37777777777" base 8 */
44 /* "4294967295" base 10 */
45 #define NUMBER_OF_DIGITS 32 /* eventually adapt if base 2 not needed */
47 #if NUMBER_OF_DIGITS < 32
48 # warning _ltoa() and _ultoa() are not save for radix 2
51 #if defined (__SDCC_mcs51) && defined (__SDCC_MODEL_SMALL) && !defined (__SDCC_STACK_AUTO)
52 # define MEMSPACE_BUFFER __idata /* eventually __pdata or __xdata */
55 # define MEMSPACE_BUFFER
58 void __ultoa(unsigned long value
, char* string
, unsigned char radix
)
60 char MEMSPACE_BUFFER buffer
[NUMBER_OF_DIGITS
]; /* no space for '\0' */
61 unsigned char index
= NUMBER_OF_DIGITS
;
64 unsigned char c
= '0' + (value
% radix
);
65 if (c
> (unsigned char)'9')
72 *string
++ = buffer
[index
];
73 } while ( ++index
!= NUMBER_OF_DIGITS
);
75 *string
= 0; /* string terminator */
78 void __ltoa(long value
, char* string
, unsigned char radix
)
80 if (value
< 0 && radix
== 10) {
84 __ultoa(value
, string
, radix
);