Simple brute force method, is to write a routine as:
void p(char X) {
if (X < 16) {Serial.print("0");}
Serial.println(X, HEX);
}
And in the main code:
p(byte1); // etc.
Answer from JackCColeman on Stack OverflowSimple brute force method, is to write a routine as:
void p(char X) {
if (X < 16) {Serial.print("0");}
Serial.println(X, HEX);
}
And in the main code:
p(byte1); // etc.
The lowest footprint in Memory, Code and runtime would be classic bit playing
byte b;
Serial.print(b>>4, HEX);
Serial.print(b&0x0F,HEX);
Which is working fine on any 8bit type. For any other mask also the first line to
Serial.print((b>>4)&0x0F, HEX);
Print Byte Array in Serial monitor screen of Arduino IDE - Arduino Stack Exchange
How to print all the 8 bits of a byte on serial monitor, if they are all zero
How to Leading Zero's Serial print of Byte
Serial.print(13, BYTE); equivalent command in Arduino 1.0
Videos
Serial.write is more down to earth , it is simple and fast, it is made to talk binary, one byte at a time. example:
Serial.write(0x45); // will write 0100 0101 to the cable
Serial.print in the other hand is more versatile , it will do the conversion for you from ASCII to binary it also can convert to BIN/ HEX/OCT/DEC but you need to specify a second argument like so
Serial.print(76, BIN) gives "0100 1100"
Serial.print(76, OCT) gives "114"
Serial.print("L", DEC) gives "76"
Serial.print(76, HEX) gives "4C"
more examples with visual serial output :
Code:
Serial.write(0x48); // H
Serial.write(0x45); // E
Serial.write(0x4C); // L
Serial.write(0x4C); // L
Serial.write(0x4F); // O
SERIAL OUTPUT :

Code:
Serial.print("HELLO");
SERIAL OUTPUT :

Serial.println() in the other hand will add end of line 2 bytes 0x0D and 0x0A as you can see in the frame
Code:
Serial.println("HELLO");
SERIAL OUTPUT :

From the Arduino site for Serial.write and Serial.print:
Serial.write()
Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.
Serial.print()
Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is
The Arduino print / println function casts the int to a long, which is 4 bytes long for Arduinos. See here: https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Print.cpp
To have more control over printing check out the C++ sprintf function. For example,
int x = 0x9876;
char buf[9];
sprintf(buf, "%04x", x);
Serial.println(buf);
Will print it out correctly.
sprintf - http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
format string reference - http://www.cplusplus.com/reference/clibrary/cstdio/printf/
Some implementations of the Print class, include printf (Like Adafruit's, see here). In that case, you just do Serial.printf("%04X", x).
This is because the data-type 'int' in 'C' language, as per ANSI C standards, is a 32-bit numeric type, and thus you see 4 bytes.
If your variable 'x' can only take 2 byte values, then declare it as a 'short' variable, instead of 'int'. This is because 'short' type as per the language standards is a 16-bit numeric type.