Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; 'sys': 0.05; 'bits': 0.07; 'nicely': 0.07; 'attribute.': 0.09; 'endian': 0.09; 'modules.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:ctypes': 0.09; 'unsigned': 0.09; 'bug': 0.10; 'python': 0.11; 'skip:f 30': 0.15; 'clear.': 0.16; 'endian,': 0.16; 'expecting': 0.16; 'fails"""': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'runs': 0.18; 'ctypes': 0.22; 'object.': 0.22; 'module': 0.23; 'import': 0.24; 'seems': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'skip:f 40': 0.27; 'device': 0.29; "i'm": 0.29; 'looks': 0.29; 'cat': 0.29; 'workaround': 0.29; 'array': 0.29; 'random': 0.29; "skip:' 10": 0.30; 'becomes': 0.31; 'mention': 0.31; 'version,': 0.31; 'writes': 0.31; 'print': 0.31; 'source': 0.31; 'code': 0.31; 'creating': 0.32; 'skip:_ 10': 0.32; 'class': 0.33; 'problem': 0.33; 'int': 0.33; 'another': 0.34; 'file': 0.34; 'skip:c 30': 0.35; 'to:addr:python-list': 0.35; 'done': 0.35; 'list': 0.35; 'but': 0.36; 'thanks': 0.36; 'subject:: ': 0.37; "won't": 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'your': 0.60; 'hope': 0.61; 'skip:u 10': 0.62; 'more': 0.62; 'mar': 0.65; 'walk': 0.72; 'construct': 0.84; 'cpu,': 0.84; '2014,': 0.91; 'slipped': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: ctypes and byte order Date: Thu, 18 Jun 2015 12:33:08 +0200 Organization: None References: <818768874.4716193.1434617881522.JavaMail.root@sequans.com> <1972345947.4721181.1434620395686.JavaMail.root@sequans.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8c90.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434623611 news.xs4all.nl 2852 [2001:888:2000:d::a6]:42271 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92814 Jean-Michel Pichavant wrote: > I'm currently writing python code that writes a small binary file to be > used by another device which code is written in C. The python code runs on > a little endian CPU, and unfortunately, the other device is using a big > endian MIPS. > > My problem is the following: I cannot make an array of n int work in big > endian, I can workaround the problem by creating n intergers, which is > nicely done thanks to list comprehension but that seems to me like a > misuse of the ctypes modules. > > ctypes is expecting a 'c_uint_be_Array_5' (note the _be_ for big endian), > and I don't know how to construct such object. > > I hope I am clear. Yes, very clear, thanks for the effort, although the nitpicker in me has to mention that an off-by-one bug slipped into your code ;) > class Foo_be_working(ctypes.BigEndianStructure): > """Working big endian version, looks more like a workaround.""" > _fields_ = [('bar%i'%i, ctypes.c_uint32) for i in range(5)] [...] > f_be = Foo_be_working(0,1,2,3,4) > print "bar0 and bar5: ", f_be.bar0, f_be.bar5 I'm not very familiar with ctypes, but a random walk through the source found the _endian module and the __ctypes_be__ attribute. So > > iarray = ctypes.c_uint32*5 # array of 5 unsigned 32 bits > > class Foo(ctypes.Structure): > """Native byte order, won't work on my big endian device""" > _fields_ = [('bar', iarray)] > > class Foo_be(ctypes.BigEndianStructure): > """big endian version, but python code fails""" > _fields_ = [('bar', iarray)] 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.