2

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.

asked Aug 26, 2012 at 23:44

1 Answer 1

6

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.

answered Aug 26, 2012 at 23:47
Sign up to request clarification or add additional context in comments.

1 Comment

You are my hero. I must have looked right over that; A little while ago, I had to use little endian by using a '<', can't believe I missed that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.