Binary Data Encoding (cont)

Unpacking data with unpack(fmt, string)

  • Same idea in reverse.
  • Returns a tuple of unpacked values
     t = struct.unpack("hhii",s)
     a,b,c,d = struct.unpack("hhii",s)

Data alignment and bit ordering

  • First character of format string can specify encoding rules
     '@'        Native byte order     Native size and alignment
     '='        Native byte order     Standard size and alignment
     '<'        Little endian         Standard size and alignment
     '>'        Big endian            Standard size and alignment
     '!'        Network order         Standard size and alignment
  • Native alignment uses the size and alignment rules of the C compiler.
  • Standard alignment uses no padding and assumes the following sizes
     short    2 bytes          int      4 bytes
     long     4 bytes          float    32 bits
     double   64 bits
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 110
July 17, 2000, beazley@cs.uchicago.edu
>>>