From: bela Date: Mon, 12 Aug 2002 14:06:22 +0000 (+0000) Subject: added functions _itoa _uitoa X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=e7ab25867557920706a6fed1767af2e299dce661;p=fw%2Fsdcc added functions _itoa _uitoa git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2073 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/device/lib/_itoa.c b/device/lib/_itoa.c new file mode 100755 index 00000000..b282dac6 --- /dev/null +++ b/device/lib/_itoa.c @@ -0,0 +1,49 @@ +/*------------------------------------------------------------------------- + integer to string conversion + + Written by: Bela Torok, 1999 + bela.torok@kssg.ch + usage: + + _uitoa(unsigned int value, char* string, int radix) + _itoa(int value, char* string, int radix) + + value -> Number to be converted + string -> Result + radix -> Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex) +---------------------------------------------------------------------------*/ + +#define NUMBER_OF_DIGITS 16 + +void _uitoa(unsigned int value, char* string, unsigned char radix) +{ +unsigned char index, i; +/* char buffer[NUMBER_OF_DIGITS]; */ /* space for NUMBER_OF_DIGITS + '\0' */ + + index = NUMBER_OF_DIGITS; + i = 0; + + do { + string[--index] = '0' + (value % radix); + if ( string[index] > '9') string[index] += 'A' - '9' - 1; + value /= radix; + } while (value != 0); + + do { + string[i++] = string[index++]; + } while ( index < NUMBER_OF_DIGITS ); + + string[i] = 0; /* string terminator */ +} + +void _itoa(int value, char* string, unsigned char radix) +{ + if (value < 0 && radix == 10) { + *string++ = '-'; + _uitoa(-value, string, radix); + } + else { + _uitoa(value, string, radix); + } +} +