ctypes and byte order

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Jun 18 07:25:35 EDT 2015


----- Original Message -----
> From: "Peter Otten" <__peter__ at web.de>
> becomes
> 
> $ cat be2.py
> import ctypes, sys
> 
> iarray_be = ctypes.c_uint32.__ctype_be__*5
> 
> class Foo_be(ctypes.BigEndianStructure):
>         _fields_ = [('bar', iarray_be)]
> 
> print sys.version
> f_be = Foo_be((0,1,2,3,0x11223344))
> print hex(f_be.bar[4])
> 
> $ python be2.py
> 2.7.6 (default, Mar 22 2014, 22:59:56)
> [GCC 4.8.2]
> 0x11223344L
> 
> which might do what you want.

Brilliant !

I've tested it and it yields the exact same results (binary file content wise) than my "workaround" structure.
But that's way better since my actual structure is more complex and arrays will be required.

Though I'm slightly puzzled by the ctypes author(s) choice, this is not documented and requires to peek into the source code. Dunder attributes are rarely part of an interface.

Anyway, thanks for your help !

JM



-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list