5

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?

user200783
  • 13,722
  • 12
  • 69
  • 135

1 Answers1

8

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.

Torben Klein
  • 2,943
  • 1
  • 19
  • 24
  • 4
    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` – Sam Mason Jan 07 '19 at 12:46