long data / Serial.print


when I send

long l = 123456;

to a Python script I have to read 6 (count of numbers) bytes instead of 4 (size in byte of data type long)

print ser.read(6)

so it looks Serial.print send every single number as a byte instead the correct 4 bytes.

Is there a option to choose or do I have to take care of this my self?


Serial.print formats the number into a series of ASCII characters and sends those. Use serial.write to send binary data.


long l = 135274560;
Serial.write(l >> 7);
Serial.write(l >> 14);
Serial.write(l >> 21);


for (int i=0;i<sizeof(l);i++){
  Serial.write(l >> i*7);

is there a better way?

That won't work. Why would you shift by 7?

Try Serial.write(&l, 4);

That won't work.

Actually it does.

Why would you shift by 7?

I don't know. I think it should be shifted by 8. But then it doesn't work.

Try Serial.write(&l, 4);


no matching function for call to 'HardwareSerial::write(long int*, int)'
no matching function for call to 'HardwareSerial::write(long int*, int)'

So, cast it.


Serial.write ((char *)&l, 4); //or perhaps (byte *)

Serial.write ((char *)&l, 4);

Great, it works

Thank you!