0

So i have struct:

typedef struct sendPacketStruct {
  byte header = headerByte;//0x55
  unsigned int time;//2 bytes
  int height;//2 bytes
};

And i do this:

    sendPacketStruct sendPacket;

    sendPacket.time = 84;//0x54
    sendPacket.height = 100;//0x65
    
    Serial.write((byte*)&sendPacket, sizeof(sendPacket));

But when i receive it at the other end the hex bytes are:

55 54 00 64 00

So the first byte is correct as the header 0x55, but the 2x 2 byte values time and height are there but shifted.

eg it should be 00 54 not 54 00

8
  • it should be 00 54 not 54 00 ... why? ... is it causing a problem? ... the bytes are not shifted – jsotola Aug 15 '20 at 4:36
  • Because if i read out the 2nd and 3rd byte to make the int I would get 0x5400 which is 21504 not 84 (0x54) – Hayden Thring Aug 15 '20 at 4:56
  • 1
    see 'endiannes' in wikipedia. this is why it is better to send text between systems. – Juraj Aug 15 '20 at 4:59
  • 2
    Either agree on an endian-ness (for transmission) among all systems sharing this data, or include an endian-ness indicator in the packet, and all receiver's agree to receive accept either one. – JRobert Aug 15 '20 at 14:37
  • 1
    Note that today most CPUs, including AVR, ARM and x86-64, are little endian. – Edgar Bonet Aug 15 '20 at 18:43
0

The answer is that arduino is little endian, so the byte order is correct. I was just expecting big endian order as ive seen in some other network protocols. If that is desired instead I can just do some bit shifting and re-arranging.

1
  • The endianness of a protocol is defined by the protocol. In C there are functions htons() ntohs() etc for "Host to Network Short" and so on that set the correct endianness for the protocol. – Majenko Aug 16 '20 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.