pack.calcsize('1b1I') = 1+4=8 !?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

pack.calcsize('1b1I') = 1+4=8 !?

Post by VladVons » Tue Mar 03, 2020 6:09 am

Code: Select all

    # size of byte is 1
    print(struct.calcsize('1b'))
    1

    # size of integer is 4
    print(struct.calcsize('1I'))
    4

    # size of byte + integer should be 5, but got 8
    print(struct.calcsize('1b1I'))
    8

User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: pack.calcsize('1b1I') = 1+4=8 !?

Post by jimmo » Tue Mar 03, 2020 6:19 am

Please see https://docs.python.org/3/library/struc ... -alignment

Your example is the same as using the @ specifier, which means "native" alignment, hence it works like a regular C struct, where each member will be aligned.

Use the < or > specifier (which you should do anyway to be clear about which endianness you're using)

Code: Select all

print(struct.calcsize('<1b1I'))
5

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: pack.calcsize('1b1I') = 1+4=8 !?

Post by VladVons » Tue Mar 03, 2020 6:22 am

oh yep, thanks!

Post Reply