Modified print library

Hi all,
I was annoyed by necessity to use sprintf(buffer...) xxx.print(buffer), when I wanted to have especially digits to have correctly formatted. As many of .print functions (serial, lcd etc.) inherit from print library, I modified the print library with another parameter, which allows (at all unsigned types-i.e. char, int, long) to print with fixed numer of digits and selectable prefix character. For example,

lcd.print((unsigned)i, DEC, FILL_ZEROS | 5);

if i is 123, for example, the printed output will be 00123. If the third parameter is omitted, is behaves like a standard print library included with IDE. Other option is FILL_SPACES.

Print.cpp (5.4 KB)

Print.h (2.93 KB)

hi,
you should ask to implement your patch here: Google Code Archive - Long-term storage for Google Code Project Hosting. so the next IDE version will have something yours!

Did you test the performance and footprint of your version of the print lib?

It is indeed a feature request that comes up every now and then. Can it also fill with spaces iso 0's?

You can use either FILL_SPACES or FILL_ZEROS. As for the footprint, when I recompiled my sketch with change from sprintf(buffer...) to my mod print lib, the sketch went from approx. 4k to approx. 3k. Except of passing one more parameter to the function call, there is almost none performance penalty. The change is surprisingly small. You can look at the source code and try the modified and unmodified library, the sources are available.

didn't you overloaded the mothod?