I'm working with V_88 to help him to get one Arduino to send data to another Arduino via XBee attached to a SoftwareSerial object, but my question would also apply to hardware serial. I'm advising him with one method of transmitting and receiving a structured data format. I am trying to use serial.print(var, HEX); to send a byte of data encoded as two ASCII characters. Unfortunately, I'm noticing that if the high nybble of the byte of data is zero then the print method will strip the leading zero. For example,
serial.print(0x23, HEX); would send the ASCII character '2' and then the ASCII character '3'. This is good. But,
serial.print(0x03, HEX); would only send the ASCII character '3'. Goodbye structured data format.
Is there a way to have serial.print(var, HEX); not strip leading zeros for any datatype of var, or would we best be served by rolling our own function to manually convert the nybbles to char and serial.write them?
For the insanely curious, see this post (and the surrounding posts) for context.
Edit: Add [Solved] to the subject.