Jacob Valenta
Jacob Valenta

Reputation: 6769

python structs, unwanted extra bytes

I am writing a bitmap file using python. My code to create the bitmap header is:

pack('bblll', 66, 77, fileLength, 0, 122)

Unless my math is wrong, (Which, it's not) this should create a string of bytes that is 14 bytes long.

b = char (1 byte) l = long (4 bytes)

1 + 1 + 4 + 4 + 4 = 14

But, the thing is, I receive 2 extra bytes; Say I have a filelength of 142, I would receive the following (in hexidecimal):

42 4D 00 00 8E 00 00 00 00 00 00 00 7A 00 00 00

The 2 extra bits are nulls at offset 0x2 and 0x3. Where do They come from? And How do I get rid of them? It is corrupting my images.

Upvotes: 2

Views: 623

Answers (1)

Alexander Gessler
Alexander Gessler

Reputation: 46607

It pads to four-byte (word) boundaries so the longs start at natural offsets (i.e. offsets that are a multiple of four), use the = prefix (or any other of the possible byte order prefixes) to prevent this.

More info on this documentation page.

Upvotes: 6

Related Questions