1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-30 11:24:12 +01:00

Optimizing printing of numbers (writing a single buffer).

Fix from Bill Greiman via Limor.
This commit is contained in:
David A. Mellis 2011-05-07 18:17:32 -04:00
parent cf044cd236
commit 1747292711

View File

@ -179,25 +179,23 @@ void Print::println(double n, int digits)
// Private Methods /////////////////////////////////////////////////////////////
void Print::printNumber(unsigned long n, uint8_t base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
void Print::printNumber(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
if (n == 0) {
print('0');
return;
}
*str = '\0';
while (n > 0) {
buf[i++] = n % base;
// prevent crash if called with base == 1
if (base < 2) base = 10;
do {
unsigned long m = n;
n /= base;
}
char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
for (; i > 0; i--)
print((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
write(str);
}
void Print::printFloat(double number, uint8_t digits)