Python Struct module behaves strange -



Python Struct module behaves strange -

i'm using struct module, , things aren't going expected. due misunderstanding have module i'm sure.

import struct s = struct.struct('q'); print s.size s = struct.struct('h l q'); print s.size s = struct.struct('h q'); print s.size s = struct.struct('h q h'); print s.size

the output of is:

8 24 16 18

what missing here? why sec , 3rd different sizes, , why 4th not 16?

alignment issue.

assuming you're running on 64-bit non-windows platform: q , l 8-byte long, 4-byte, , h 2-byte.

and these types must set on location multiple of size best efficiency.

therefore, 2nd struct arranged as:

hh______ llllllll qqqqqqqq

the 3rd struct:

hh__iiii qqqqqqqq

and 4th struct:

hh__iiii qqqqqqqq hh

if don't want alignment, , require l have 4 byte (the "standard" size), you'll need utilize = or > or < format, described in http://docs.python.org/library/struct.html#struct-alignment:

import struct s = struct.struct('=q') print s.size s = struct.struct('=hlq') print s.size s = struct.struct('=hiq') print s.size s = struct.struct('=hiqh') print s.size

demo: http://ideone.com/emlgm

python

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -