Serial.println(x, BIN); // print as an ASCII-encoded binary
If the type for x is byte I expect the above to print out eight characters, 00110011.
But that is not what happens. Only 110011 is printed out.
If that wasn't bad enough the following floored me.
Serial.println(~x, BIN);
produces
FFFFFFCC
Wouldn't it make sense for the output to reflect the type (size) of data that is being printed? From what I recall the same problem also exists for HEX. (and likely Octal)
There are overloaded function definitions for print, println in the class (see the Print.h). Compiler can choose inappropriate println version in such cases, with the unsigned long parameter in your case and the rest before byte is filled with zeros (00000033h -> ~FFFFFFCCh). The solution is to use type cast which should force the compiler to choose required version of function. Like this: Serial.println((unsigned char)(~x), BIN); .
Rudy216:
Actually it was a signed long. When I printed it without the HEX specifier it printed as a negative number.
Of course, it's possible. It could depend on x definition, if it is signed or unsigned or something else like optimizing process and the way how the lib is written. Basically, there are 2 base versions of print() for integer - long and unsigned long, all other versions are derived println() included.
Is it possible to implement Serial.printf() as C language's printf() does ?
If so, we use %d for integer, %s for string, %f for float point data, etc.
We can print different data type in the same function call.
We also can print many data in the same code line.
flyhawk001:
Is it possible to implement Serial.printf() as C language's printf() does ?
If so, we use %d for integer, %s for string, %f for float point data, etc.
We can print different data type in the same function call.
We also can print many data in the same code line..
It contains my own implementation which includes floating point data.
You can also use streaming in and out.
It is used to add extra functionality to any Stream or Print based library (Serial, Ethernet, SD, LCD, etc...)