Go Down

Topic: Serial.print different data types (Read 2990 times) previous topic - next topic

Rudy216

Nov 22, 2015, 11:01 pm Last Edit: Nov 22, 2015, 11:04 pm by Rudy216
 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)

Budvar10

#1
Nov 23, 2015, 08:31 am Last Edit: Nov 23, 2015, 08:32 am by Budvar10
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); .
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Rudy216

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.

Budvar10

#3
Nov 24, 2015, 08:41 am Last Edit: Nov 24, 2015, 08:42 am by Budvar10
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.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

PaulS

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.
The art of getting good answers lies in asking good questions.

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.


pYro_65

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-

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...)
Forum Mod anyone?
https://arduino.land/Moduino/

Go Up