Skip to content

Commit e2f54b1

Browse files
committed
fixed for 10 digit numbers now working for full long int range
1 parent 0133403 commit e2f54b1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Diff for: cores/arduino/Print.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ size_t Print::println(const Printable& x)
202202

203203
size_t Print::printNumber(unsigned long n, uint8_t base)
204204
{
205-
// shortcut printing just 0 prevents later overhead
205+
// shortcut printing just 0 and prevent later overhead
206206
if (n == 0) {
207207
return write('0');
208208
}
@@ -212,6 +212,10 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
212212

213213
unsigned long reverse = 0;
214214
uint8_t digits = 0;
215+
char avoid_overflow = n % base;
216+
217+
// this step and 'avoid_overflow' will make sure it stays in unsigned long range beeing able to print all 10 digits no matter what
218+
n /= base;
215219

216220
// reverse the number and count digits
217221
while (n != 0) {
@@ -222,13 +226,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
222226
}
223227

224228
// from here onwards reuse of variable 'n' to count written chars
225-
do {
229+
while (digits--) {
226230
char c = reverse % base;
227231
reverse /= base;
228232

229233
c = (c < 10 ? c + '0' : c + 'A' - 10);
230234
n += write(c);
231-
} while(--digits);
235+
}
236+
237+
avoid_overflow = (avoid_overflow < 10 ? avoid_overflow + '0' : avoid_overflow + 'A' - 10);
238+
n += write(avoid_overflow);
232239

233240
return n;
234241
}

0 commit comments

Comments
 (0)