Main Content

Binary data handling - Python and Perl

Archive - Originally posted on "The Horse's Mouth" - 2015-03-09 13:46:53 - Graham Ellis

Handling binary data has become a somewhat rarer requirement over tiem, but that doensn't mean the need has gone away - and on last week's Python course, my delegates had a requirement to read data in this format.

First and foremost with binary data, you need to understand what you're looking to read and write, and it's likely to vary from one person to the next. As an example when I'm training, I demonstrate the functionallity of reading binary data by reading the headers of a .gif file - where the first 10 bytes are:
  • the letters GIF
  • the subtype (87a or 89a) as 2 characters
  • 16 bit integer, big endian, image width
  • 16 bit integer, big endian, image height

Here's my sample code - open the file and read - which will default to reading the whole file:

  fh = open("amazon.gif","r")
  bytes = fh.read()


Then from within the struct module interpret the bytes, as described above:

  type, subtype, width, height= struct.unpack("3s3shh",bytes[:10])

Sample output:

  trainee@kingston:~/m15py$ python imgsize
  Image is 90 x 28
  Image file size is 797 bytes
  trainee@kingston:~/m15py$


Complete sources [here].

In writing this article, I looked up some of the previous examples I've written to handle biary data, and came across the same interprettation in Perl - see [here], with illustrations from a course I gave a couple of years back in Nurmberg, and with further examples of Perl's pack and unpack [here].