Python struct.pack to arduino C

Hi experts,

I am trying to port a python program to an arduino and I strugle to create a network packet. This packet is constructed by struct.pack python routine.
The packet is constructed with this struct.pack argument : !BBHIII16s
Do you know how to build such binary format ?

Regards,

!BBHIII16s

reads as

!

= network (big endian)

BB

= 2 x unsigned char (2 x 1 byte)

H

= unsigned short (1 x 2 bytes)

III

= 3 x unsigned int (3 x 4 bytes)

16s

= char --> char array of 16 bytes

struct __attribute__ ((packed)) {
  uint8_t c1;  // 1 byte   - @0
  uint8_t c2;  // 1 byte   - @1
  uint16_t s1; // 2 bytes  - @2 and 3
  uint32_t i1; // 4 bytes  - @4 ,5, 6 and 7
  uint32_t i2; // 4 bytes  - @8, 9, 10, 11
  uint32_t i3; // 4 bytes  - @12 ,13, 14 and 15
  char t[16];  // 16 bytes - @16 to 31
} data;

remember your arduino is little endian so the unsigned short (uint16_t) and unsigned int (uint32_t) will likely need tweaking (permuting bytes) -> read the incoming 32 bytes in a byte buffer, then (counting from 0) flip bytes 2 and 3, and reverse 4 to 7, 8 to 11 and 12 to 15 so that they are in the right little endian mode. Then you can memcpy() the buffer into your struct and all should be aligned (with gcc)

Thank you very much ! It worked flawlessly !
I used the bswap builtin to flip the endianess __builtin_bswap16(number);

Regards,

Bertrand

Great !