ezieragabriel / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Serial.print as HEX should ALWAYS print 2 chars per byte; it omits leading zeroes #1097

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.If byte or integer variables are printed with serial.print or serial.println 
with the HEX output format option leading zeroes are not printed:
byte reading5 = 0x0F;
byte reading6 = 0x00;
byte reading7 = 0xF0;
.............
    Serial.print("reading5 = ");
    Serial.println(reading5, HEX);
    Serial.print("reading6 = ");
    Serial.println(reading6, HEX);
    Serial.print("reading7 = ");
    Serial.println(reading7, HEX);

-------------------
and the result is:
reading5 = F
reading6 = 0
reading7 = F0

What is the expected output? What do you see instead?

The output for HEX should always be one char per nibble; leading zeroes matter. 
With the data above, calling:
Serial.print("0x");
Serial.print(reading5, HEX);
Serial.print(reading6, HEX);
Serial.println(reading7, HEX);

yields 0xF0FO where it should display 0x0F00FO; the value is totally changed by 
omission of the leading zero. 

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?
Arduino 1.0.1, WinXP (current, fully updated), Duemilinove

Please provide any additional information below.

Same behaviour with Arduino 1.0 and using the Arduino serial monitor or 
SecureCRT interface.

I can't think of any reason to omit leading zeroes when using the HEX format. 
In cases like this, where a sequence of bytes is read from a device that 
represents a multi-byte return value, and you want to see the real value 
returned it gives incorrect results. One could assemble the bytes into a LONG 
and then call the function, but I think that most who use the HEX format would 
like to see all their nibble values with any size data. The examples in the 
Arduino reference sidestep the issue by not showing any example with a leading 
zero ;-)
http://arduino.cc/en/Serial/Print

Original issue reported on code.google.com by dale.cal...@gmail.com on 5 Nov 2012 at 10:22

GoogleCodeExporter commented 9 years ago
As a previous victim of this (Now having code like:

if( d < 0x10){ Serial.print("0");} Serial.print(d,HEX);,

Notice this is even worse if the argument is int or long.

Perhaps instead, to allow a single digit HEX  and maintain backward 
compatibility,
new types  HEX02, HEX03, HEX04, ... HEX08 could be added providing a much as 
00000001 for the HEX08 case.

Surely the documentation MUST be revised to include this FEATURE.
For those who feel to much documentation confuses the beginner perhaps a 
"addition details" secondary documentation page is appropriate.

Original comment by jaguar3s...@gmail.com on 6 Nov 2012 at 6:41

GoogleCodeExporter commented 9 years ago
Issue 549 as old as May 24, 2011 suggested 

Serial.print(n, HEX, 3);

Original comment by jaguar3s...@gmail.com on 6 Nov 2012 at 6:48

GoogleCodeExporter commented 9 years ago
Before print() gets completely huge, perhaps we should just import/allow 
printf() ?

avr-size /tmp/applet/Print.cpp.o         
   text    data     bss     dec     hex filename
   1710       2       0    1712     6b0 /tmp/applet/Print.cpp.o
avr-size /sw/cross/avr/lib/libprintf_min.a 
   text    data     bss     dec     hex filename
    687       0       0     687     2af vfprintf_min.o (ex /sw/cross/avr/lib/libprintf_min.a)
avr-size /sw/cross/avr/lib/libprintf_flt.a 
   text    data     bss     dec     hex filename
   1912       0       0    1912     778 vfprintf_flt.o (ex /sw/cross/avr/lib/libprintf_flt.a)

Original comment by wes...@gmail.com on 7 Nov 2012 at 1:39