struct is saving 4 bytes instead of 2

TonyB tigerb at yahoo.com
Wed Sep 12 19:36:18 EDT 2007


On Sep 12, 6:45 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n... at spamgourmet.com> wrote:
> TonyB wrote:
> > When I inspect the file with a hex editor it show that the
> > variable is being saved as 4 bytes.  How can I make it save the
> > value as 2 bytes?
>
> Use an aligment specifier in the format string (you want "standard"
> alignment). See:
>
> http://docs.python.org/lib/module-struct.html
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #428:
>
> Firmware update in the coffee machine

Björn,

I tried

f.write(struct.pack('=h',thevariable))

and it works.  Many thanks,
Tony






More information about the Python-list mailing list