Arduino Forum

Development => Suggestions for the Arduino Project => Topic started by: Rudy216 on Nov 22, 2015, 11:01 pm

Title: Serial.print different data types
Post by: Rudy216 on Nov 22, 2015, 11:01 pm
 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)
Title: Re: Serial.print different data types
Post by: Budvar10 on Nov 23, 2015, 08:31 am
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); .
Title: Re: Serial.print different data types
Post by: Rudy216 on Nov 24, 2015, 01:29 am
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.
Title: Re: Serial.print different data types
Post by: Budvar10 on Nov 24, 2015, 08:41 am
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.
Title: Re: Serial.print different data types
Post by: PaulS on Nov 24, 2015, 01:39 pm
Quote
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.
Title: Re: Serial.print different data types
Post by: flyhawk001 on Dec 28, 2015, 06:32 pm
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.

Title: Re: Serial.print different data types
Post by: pYro_65 on Jan 01, 2016, 02:33 am
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: https://github.com/Chris--A/PrintEx#printex-library-for-arduino- (https://github.com/Chris--A/PrintEx#printex-library-for-arduino-)

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