473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7024
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Taylor Howell | last post by:
Hello all, I have a delima. I have 8 5bit numbers that I need to pack into one (or more) variables. They then must be written (exactly 40bits (5Bytes)) to a file and have the ability to be put...
4
by: James Roberge | last post by:
I am having a little trouble getting my union/struct to work correctly. I am creating a struct that will contain information about the status of various Z80 cpu registers in an emulator i am...
8
by: Luc Le Blanc | last post by:
I have 2 APIs that store/recall a void *. Since all I need to store is a 32-bit struct, I pass the actual data (instead of a pointer to it) as a void *: typedef { UInt8 color;...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
12
by: vishnupriya.sureshbabu | last post by:
What is the main reason for prefering class over struct? Access specifires can also be used in struct. Is there any other reason for using class other than inheritance?
2
by: sygnosys | last post by:
Hi, Does C# support struct bit definitions. For example is there a way to declare somethink like this C/C++ Code: struct CELL { // Declare CELL bit field unsigned short character : 8; //...
3
by: abhivg | last post by:
Hi, I am trying to port a 32 bit Unix application to 64 bit Windows. While compiling on Windows I am getting a number of warnings related to structure padding. More specifically "warning C4820:...
16
by: =?iso-8859-1?Q?Daniel_Lidstr=F6m?= | last post by:
Hello! I have these following classes: public struct Move { public int From { get; set; } public int To { get; set; }
1
by: tytelizgal | last post by:
Hello, Here is my problem: I declare the vector struct in my .h file and then try to access its fields in the .c file. I did declare a variable of type vector. However, I get a compiler error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.