refactored printIntegerInBase to work without a buffer + minor cleanup
This commit is contained in:
21
print.c
21
print.c
@ -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)
|
||||
|
Reference in New Issue
Block a user