¿ªÔÆÌåÓý

Re: Pulling Arduino data apart


 

On Thu, Mar 8, 2018 at 1:42 PM, Jerry Gaffke via Groups.Io <jgaffke@...> wrote:
Here's C code for machine A to send a 32 bit integer as a sequence of four bytes in little endian order::
? ? sendbyte(data32);? sendbyte(data32>>8);? sendbyte(data32>>16);? sendbyte(data32>>24);
And code for machine B to receive that 32 bit integer (assumes getbyte() returnes an unsigned 8 bit integer):
? ? data32=getbyte();? data32|=getbyte()<<8;? data32|=getbyte()<<16; data32|=getbyte<<24;

This C code doesn't care if the machine it is on is big endian or little endian.
However the C code on both ends must be aware of the integer size it is dealing with, be it 8,16,32 bits.
So the serial link spec may need to fully define the format of the data stream, not just say whether
it is big or little endian.>? Your?sendbyte()?example, the sendbyte(data32>>24) leaves the high byte for sending.

>? If you don't know the endian order, how do you know you didn't just rotate the data of interest onto the floor?

I don't quite follow.
sendbyte(data32>>24)? ?will always send the 8 msb's of that 32 bit word, regardless of what machine you are on.
I know I didn't rotate the data of interest onto the floor because I know that my data was in the 8 msb's of the 32 bit word.?
connection to a total different platform and still have it work. Knowing how to use a union is a good thing.

It seems to me like Jerry is trying to point out that the left- and right-shift operators are endian-agnostic. They work in terms of the mathematically more-significant and less-significant directions without concern for the byte-order that an architecture uses to store its integers. so 256>>8 never gives a result of 0 on any architecture, even if the internal representation is 0x00 0x01.

-Neil N0FN

Join [email protected] to automatically receive all group messages.