pycom struct.pack is order dependent
pycom struct.pack is order dependent
I'm trying to pack an epoch with a string & I can't figure out why the number of bytes depends on the order I do the packing in. Unfortunately I'm not getting any replies on the relevant microcontroller forum https://forum.pycom.io/topic/6761/struct
Re: pycom struct.pack is order dependent
it's due to alignment. A 4-byte integer needs to be aligned on a 4-byte boundary, so when you pack a byte followed by a 4-byte int, then 3 bytes of padding will be introduced.
Regular python3 behaves the same way.
Regular python3 behaves the same way.
Code: Select all
$ python3
Python 3.7.3 (default, Aug 4 2020, 19:30:55)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> bytes=struct.pack('sI', b'F', 1611017052); print(bytes, len(bytes))
b'F\x00\x00\x00\\+\x06`' 8
>>> bytes=struct.pack('Is', 1611017052, b'F'); print(bytes, len(bytes))
b'\\+\x06`F' 5
>>>
Re: pycom struct.pack is order dependent
Ahh, thnx for that, I'll stick the epoch at the front of the byte string & avoid the issue that way.