refactored printIntegerInBase to work without a buffer + minor cleanup

This commit is contained in:
Simen Svale Skogsrud
2011-06-03 21:50:02 +02:00
parent 69be1240be
commit f0843db46e
4 changed files with 15 additions and 13 deletions

21
print.c
View File

@ -40,25 +40,24 @@ void printPgmString(const char *s)
serial_write(c);
}
// Prints a single digit of any base up to 36. 0..9 prints as
// '0'..'9' while 10..35 prints as 'a'..'z'
void printDigit(uint8_t value) {
serial_write(value < 10 ?
'0' + value :
'a' + value - 10);
}
void printIntegerInBase(unsigned long n, unsigned long base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
serial_write('0');
printDigit(0);
return;
}
while (n > 0) {
buf[i++] = n % base;
printDigit(n % base);
n /= base;
}
for (; i > 0; i--)
serial_write(buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10);
}
void printInteger(long n)