Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for HEX leading zero in print #6750

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions hardware/arduino/avr/cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,41 @@ size_t Print::print(char c)
return write(c);
}

size_t Print::print(unsigned char b, int base)
size_t Print::print(unsigned char b, int base, int arg)
{
return print((unsigned long) b, base);
return print((unsigned long) b, base, arg);
}

size_t Print::print(int n, int base)
size_t Print::print(int n, int base, int arg)
{
return print((long) n, base);
return print((long) n, base, arg);
}

size_t Print::print(unsigned int n, int base)
size_t Print::print(unsigned int n, int base, int arg)
{
return print((unsigned long) n, base);
}

size_t Print::print(long n, int base)
size_t Print::print(long n, int base, int arg)
{
if (base == 0) {
return write(n);
} else if (base == 10) {
if (n < 0) {
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
return printNumber(n, 10, arg) + t;
}
return printNumber(n, 10);
return printNumber(n, 10, arg);
} else {
return printNumber(n, base);
return printNumber(n, base, arg);
}
}

size_t Print::print(unsigned long n, int base)
size_t Print::print(unsigned long n, int base, int arg)
{
if (base == 0) return write(n);
else return printNumber(n, base);
else return printNumber(n, base, arg);
}

size_t Print::print(double n, int digits)
Expand Down Expand Up @@ -149,37 +149,37 @@ size_t Print::println(char c)
return n;
}

size_t Print::println(unsigned char b, int base)
size_t Print::println(unsigned char b, int base, int arg)
{
size_t n = print(b, base);
size_t n = print(b, base, arg);
n += println();
return n;
}

size_t Print::println(int num, int base)
size_t Print::println(int num, int base, int arg)
{
size_t n = print(num, base);
size_t n = print(num, base, arg);
n += println();
return n;
}

size_t Print::println(unsigned int num, int base)
size_t Print::println(unsigned int num, int base, int arg)
{
size_t n = print(num, base);
size_t n = print(num, base, arg);
n += println();
return n;
}

size_t Print::println(long num, int base)
size_t Print::println(long num, int base, int arg)
{
size_t n = print(num, base);
size_t n = print(num, base, arg);
n += println();
return n;
}

size_t Print::println(unsigned long num, int base)
size_t Print::println(unsigned long num, int base, int arg)
{
size_t n = print(num, base);
size_t n = print(num, base, arg);
n += println();
return n;
}
Expand All @@ -200,7 +200,7 @@ size_t Print::println(const Printable& x)

// Private Methods /////////////////////////////////////////////////////////////

size_t Print::printNumber(unsigned long n, uint8_t base)
size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t arg)
{
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
Expand All @@ -209,14 +209,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)

// prevent crash if called with base == 1
if (base < 2) base = 10;

do {
//the argument is only valid for HEX values smaler than 0x10
if(base != 8 || n>=16) arg = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(base != HEX || n>=16) arg = 0;


do {
char c = n % base;
n /= base;

*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);

if(arg == 1)*--str = '0';
return write(str);
}

Expand Down
23 changes: 13 additions & 10 deletions hardware/arduino/avr/cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#endif
#define BIN 2

#define PRINT_NOARG 0
#define PRINT_LEADINGZERO 1

class Print
{
private:
Expand Down Expand Up @@ -66,23 +69,23 @@ class Print
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(unsigned char, int = DEC, int = PRINT_NOARG);
size_t print(int, int = DEC, int = PRINT_NOARG);
size_t print(unsigned int, int = DEC, int = PRINT_NOARG);
size_t print(long, int = DEC, int = PRINT_NOARG);
size_t print(unsigned long, int = DEC, int = PRINT_NOARG);
size_t print(double, int = 2);
size_t print(const Printable&);

size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(unsigned char, int = DEC, int = PRINT_NOARG);
size_t println(int, int = DEC, int = PRINT_NOARG);
size_t println(unsigned int, int = DEC, int = PRINT_NOARG);
size_t println(long, int = DEC, int = PRINT_NOARG);
size_t println(unsigned long, int = DEC, int = PRINT_NOARG);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
Expand Down