469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

struct->bit access

I am passing structs via UDP socket to my Python app from an external
C program. The structure is made up almost entirely of bit fields.

struct example:
unsigned int var1 : 3;
unsigned int var2 : 3;
unsigned int var3 : 1;
unsigned int pad1 : 1;
unsigned int var4 : 8;
unsigned int var5 : 16;

Everything I've read so far says this is too complicated and slow for
Python - write it in C. Can someone point me to a good example of
extracting this and rebuilding using only Python?
Thank you! -Mike
Jul 18 '05 #1
2 6839
On 2004-09-13, Mike Spindler <ma********@yahoo.com> wrote:
I am passing structs via UDP socket to my Python app from an external
C program. The structure is made up almost entirely of bit fields.

struct example:
unsigned int var1 : 3;
unsigned int var2 : 3;
unsigned int var3 : 1;
unsigned int pad1 : 1;
unsigned int var4 : 8;
unsigned int var5 : 16;

Everything I've read so far says this is too complicated and slow for
Python - write it in C.
That depends on how fast it needs to be. I do all sorts of
bit-field stuff in Python, and it's plenty fast enough for me.
Can someone point me to a good example of extracting this and
rebuilding using only Python?


Use &, |, << and >> operators just like you would in C

--
Grant Edwards grante Yow! My vaseline is
at RUNNING...
visi.com
Jul 18 '05 #2
It's not slow or complicated. Try something like this:
import struct

class Example(object):
def __init__(data):
'''data is a string of 4 bytes read from your input socket.'''
(i,) = struct.unpack('!I', data)
self.var1 = i & 7
self.var2 = (i >> 3) & 7
self.var3 = (i >> 6) & 1
self.var4 = (i >> 8) & 0xf
self.var5 = (i >> 16) & 0xff
You might want to change the format string for unpack() depending on the
byte order of the input.
<http://python.org/doc/2.3.4/lib/module-struct.html> has more
information.

On Mon, Sep 13, 2004 at 07:58:23AM -0700, Mike Spindler wrote:
I am passing structs via UDP socket to my Python app from an external
C program. The structure is made up almost entirely of bit fields.

struct example:
unsigned int var1 : 3;
unsigned int var2 : 3;
unsigned int var3 : 1;
unsigned int pad1 : 1;
unsigned int var4 : 8;
unsigned int var5 : 16;

Everything I've read so far says this is too complicated and slow for
Python - write it in C. Can someone point me to a good example of
extracting this and rebuilding using only Python?
Thank you! -Mike

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Taylor Howell | last post: by
8 posts views Thread by Luc Le Blanc | last post: by
60 posts views Thread by Mohd Hanafiah Abdullah | last post: by
56 posts views Thread by ccwork | last post: by
12 posts views Thread by vishnupriya.sureshbabu | last post: by
2 posts views Thread by sygnosys | last post: by
16 posts views Thread by =?iso-8859-1?Q?Daniel_Lidstr=F6m?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.