Denz Choe
Denz Choe

Reputation: 81

Why is there a difference between calcsize("!BHB") and calcsize("BHB")?

Using the struct module in python 2.4.3 why is there a difference between calcsize("!BHB") and calcsize("BHB")? whereby; when

from struct import *
calcsize("!BHB") == 4
calcsize("BHB") == 5

I understand the big endian and little endian concept, but don't really get it in terms of bytes placement for the above formats.

Upvotes: 0

Views: 612

Answers (1)

Senthil Kumaran
Senthil Kumaran

Reputation: 56813

The reason for that is explained in the Python Docs and it is due to padding issue when you are using a mixed structure members (BHB)

  1. Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.

  2. No padding is added when using non-native size and alignment, e.g. with ‘<’, ‘>’, ‘=’, and ‘!’.

See this:

>>> struct.pack("BHB",1,1,1)
'\x01\x00\x01\x00\x01'
>>> struct.pack("=BHB",1,1,1)
'\x01\x01\x00\x01'

In the first case, the padding was added to Byte because it was using the default native size and alignment and you explicitly set it standard size using '=', no padding was done.

Upvotes: 5

Related Questions