mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-02 13:24:12 +01:00
Optimizing printing of numbers (writing a single buffer).
Fix from Bill Greiman via Limor.
This commit is contained in:
parent
cf044cd236
commit
1747292711
@ -179,25 +179,23 @@ void Print::println(double n, int digits)
|
|||||||
|
|
||||||
// Private Methods /////////////////////////////////////////////////////////////
|
// Private Methods /////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void Print::printNumber(unsigned long n, uint8_t base)
|
void Print::printNumber(unsigned long n, uint8_t base) {
|
||||||
{
|
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||||
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
char *str = &buf[sizeof(buf) - 1];
|
||||||
unsigned long i = 0;
|
|
||||||
|
|
||||||
if (n == 0) {
|
*str = '\0';
|
||||||
print('0');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (n > 0) {
|
// prevent crash if called with base == 1
|
||||||
buf[i++] = n % base;
|
if (base < 2) base = 10;
|
||||||
|
|
||||||
|
do {
|
||||||
|
unsigned long m = n;
|
||||||
n /= base;
|
n /= base;
|
||||||
}
|
char c = m - base * n;
|
||||||
|
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||||
|
} while(n);
|
||||||
|
|
||||||
for (; i > 0; i--)
|
write(str);
|
||||||
print((char) (buf[i - 1] < 10 ?
|
|
||||||
'0' + buf[i - 1] :
|
|
||||||
'A' + buf[i - 1] - 10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Print::printFloat(double number, uint8_t digits)
|
void Print::printFloat(double number, uint8_t digits)
|
||||||
|
Loading…
Reference in New Issue
Block a user