Serial.print different data types

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); .

Budvar10:
Compiler can choose inappropriate println version in such cases, with the unsigned long parameter in your case

Actually it was a signed long. When I printed it without the HEX specifier it printed as a negative number.

Thank you for your answer. It is not the answer that I had hoped to get but I really do appreciate it. And I did try it and it does what it should.

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.

If the type for x is byte I expect the above to print out eight characters, 00110011.

You have unreasonable expectations, then.

If you printed 51 in decimal, you wouldn't expect 00000051, would you? Why would you expect leading 0s when printing in binary, then?

If you do expect that crap, there are ways (sprintf) to make it happen.

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..

You can use my PrintEx library, you can find it in the library manager. Or here: GitHub - Chris--A/PrintEx: An extension to the Arduino Print library, and much, much more...

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...)