added functions _itoa _uitoa
authorbela <bela@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 12 Aug 2002 14:06:22 +0000 (14:06 +0000)
committerbela <bela@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 12 Aug 2002 14:06:22 +0000 (14:06 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2073 4a8a32a2-be11-0410-ad9d-d568d2c75423

device/lib/_itoa.c [new file with mode: 0755]

diff --git a/device/lib/_itoa.c b/device/lib/_itoa.c
new file mode 100755 (executable)
index 0000000..b282dac
--- /dev/null
@@ -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);
+  }
+}
+