6

It is possible to use Python's struct.unpack to extract the useful values from a Bitmap file header, as follows:

magic, file_size, _, _, data_offset = struct.unpack('<2sLHHL', file_header)
assert magic == 'BM'

Is there any way to avoid the need to assign to _ (or another throwaway variable) here? Is it possible to change the format string to make struct.unpack skip over the two unused H fields?

1 Answer 1

9

Yes, use the "x" code to skip 1 byte. (see here: https://docs.python.org/2/library/struct.html#format-characters)

I.e., replace "H" with "xx" in the format code.

Sign up to request clarification or add additional context in comments.

1 Comment

note that you can use the count prefix to indicate an arbitrary number of bytes, i.e. 4x would discard the 4 bytes that were previously being captured by HH

Your Answer

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.