@@ -374,46 +374,52 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
374374 char buf[64 ];
375375 uint8_t i = 0 ;
376376 uint8_t innerLoops = 0 ;
377+ size_t bytes = 0 ;
377378
378- // prevent crash if called with base == 1
379- if (base < 2 ) {
380- base = 10 ;
381- }
382-
383- // process chunks that fit in "16 bit math".
384- uint16_t top = 0xFFFF / base;
385- uint16_t th16 = 1 ;
386- while (th16 < top) {
387- th16 *= base;
388- innerLoops++;
389- }
379+ // Special case workaround https://github.com/arduino/ArduinoCore-API/issues/178
380+ if (n64 == 0 ) {
381+ write (' 0' );
382+ bytes = 1 ;
383+ } else {
384+ // prevent crash if called with base == 1
385+ if (base < 2 ) {
386+ base = 10 ;
387+ }
390388
391- while (n64 > th16) {
392- // 64 bit math part
393- uint64_t q = n64 / th16;
394- uint16_t r = n64 - q * th16;
395- n64 = q;
396-
397- // 16 bit math loop to do remainder. (note buffer is filled reverse)
398- for (uint8_t j = 0 ; j < innerLoops; j++) {
399- uint16_t qq = r / base;
400- buf[i++] = r - qq * base;
401- r = qq;
389+ // process chunks that fit in "16 bit math".
390+ uint16_t top = 0xFFFF / base;
391+ uint16_t th16 = 1 ;
392+ while (th16 < top) {
393+ th16 *= base;
394+ innerLoops++;
402395 }
403- }
404396
405- uint16_t n16 = n64;
406- while (n16 > 0 ) {
407- uint16_t qq = n16 / base;
408- buf[i++] = n16 - qq * base;
409- n16 = qq;
410- }
397+ while (n64 > th16) {
398+ // 64 bit math part
399+ uint64_t q = n64 / th16;
400+ uint16_t r = n64 - q * th16;
401+ n64 = q;
402+
403+ // 16 bit math loop to do remainder. (note buffer is filled reverse)
404+ for (uint8_t j = 0 ; j < innerLoops; j++) {
405+ uint16_t qq = r / base;
406+ buf[i++] = r - qq * base;
407+ r = qq;
408+ }
409+ }
411410
412- size_t bytes = i;
413- for (; i > 0 ; i--) {
414- write ((char )(buf[i - 1 ] < 10 ?
415- ' 0' + buf[i - 1 ] :
416- ' A' + buf[i - 1 ] - 10 ));
411+ uint16_t n16 = n64;
412+ while (n16 > 0 ) {
413+ uint16_t qq = n16 / base;
414+ buf[i++] = n16 - qq * base;
415+ n16 = qq;
416+ }
417+ bytes = i;
418+ for (; i > 0 ; i--) {
419+ write ((char )(buf[i - 1 ] < 10 ?
420+ ' 0' + buf[i - 1 ] :
421+ ' A' + buf[i - 1 ] - 10 ));
422+ }
417423 }
418424 return bytes;
419425}
0 commit comments