1 /*-------------------------------------------------------------------------
2 integer to string conversion
4 Written by: Bela Torok, 1999
8 _ultoa(unsigned long value, char* string, int radix)
9 _ltoa(long value, char* string, int radix)
11 value -> Number to be converted
13 radix -> Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
14 ---------------------------------------------------------------------------*/
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 */
21 #if NUMBER_OF_DIGITS < 32
22 # warning _ltoa() and _ultoa() are not save for radix 2
25 #if defined (SDCC_mcs51) && defined (SDCC_MODEL_SMALL) && !defined (SDCC_STACK_AUTO)
26 # define MEMSPACE_BUFFER __idata /* eventually __pdata or __xdata */
29 # define MEMSPACE_BUFFER
32 void _ultoa(unsigned long value, char* string, unsigned char radix)
34 char MEMSPACE_BUFFER buffer[NUMBER_OF_DIGITS]; /* no space for '\0' */
35 unsigned char index = NUMBER_OF_DIGITS;
38 unsigned char c = '0' + (value % radix);
46 *string++ = buffer[index];
47 } while ( ++index != NUMBER_OF_DIGITS );
49 *string = 0; /* string terminator */
52 void _ltoa(long value, char* string, unsigned char radix)
54 if (value < 0 && radix == 10) {
58 _ultoa(value, string, radix);