Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / lib / __ltoa.c
blob8176081f453e660b70626268cf9d9701481ae8f6
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
9 later version.
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,
19 MA 02110-1301, USA.
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 /*-------------------------------------------------------------------------
30 usage:
32 __ultoa(unsigned long value, char* string, int radix)
33 __ltoa(long value, char* string, int radix)
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 ---------------------------------------------------------------------------*/
40 #include <stdlib.h>
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
49 #endif
51 #if defined (__SDCC_mcs51) && defined (__SDCC_MODEL_SMALL) && !defined (__SDCC_STACK_AUTO)
52 # define MEMSPACE_BUFFER __idata /* eventually __pdata or __xdata */
53 # pragma nogcse
54 #else
55 # define MEMSPACE_BUFFER
56 #endif
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;
63 do {
64 unsigned char c = '0' + (value % radix);
65 if (c > (unsigned char)'9')
66 c += 'A' - '9' - 1;
67 buffer[--index] = c;
68 value /= radix;
69 } while (value);
71 do {
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) {
81 *string++ = '-';
82 value = -value;
84 __ultoa(value, string, radix);