Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Interconvert a ctypes.Structure to/from a binary string?

Reply
Thread Tools

Interconvert a ctypes.Structure to/from a binary string?

 
 
Andrew Lentvorski
Guest
Posts: n/a
 
      08-02-2008
Basically, I'd like to use the ctypes module as a much more descriptive
"struct" module.

Is there a way to take a ctypes.Structure-based class and convert it
to/from a binary string?

Thanks,
-a
 
Reply With Quote
 
 
 
 
Uwe Schmitt
Guest
Posts: n/a
 
      08-02-2008
On 2 Aug., 08:35, Andrew Lentvorski <(E-Mail Removed)> wrote:
> Basically, I'd like to use the ctypes module as a much more descriptive
> "struct" module.
>
> Is there a way to take a ctypes.Structure-based class and convert it
> to/from a binary string?
>
> Thanks,
> -a


My first idea was :

from ctypes import *
from pickle import *

class T(Structure): pass

print dumps(T())

which raises an TypeError, "abstract class".

And then I found

http://osdir.com/ml/python.ctypes/2006-03/msg00009.html

Greetings, Uwe

 
Reply With Quote
 
 
 
 
Andrew P. Lentvorski, Jr.
Guest
Posts: n/a
 
      08-02-2008
On Aug 1, 11:35 pm, Andrew Lentvorski <(E-Mail Removed)> wrote:
> Basically, I'd like to use the ctypes module as a much more descriptive
> "struct" module.
>
> Is there a way to take a ctypes.Structure-based class and convert it
> to/from a binary string?
>
> Thanks,
> -a


After chugging through the ctypes source code, I found that I can
abuse ctypes into doing what I want.

Here is how I did it. I can abuse
string_at(addressof(SomeCtypesClass), length) to get a binary string
out of ctypes while I use:

def analyze_elf_header(binaryData):
headerSize = ctypes.sizeof(Elf32_Ehdr)
header = Elf32_Ehdr()

# Abuse ctypes to initialize from a string
bb = ctypes.create_string_buffer(binaryData[0:headerSize])
ctypes.memmove(ctypes.addressof(header), ctypes.addressof(bb),
headerSize)

To jam stuff into a ctypes class. This seems like an oversight in the
module though. It would really be better if the class itself had
methods to init from/produce to a binary string.

However, I would prefer that somebody who actually knows ctypes to
weigh in here with comments about what I did.

Thanks,
-a
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
x86 binary runs; x86_64 binary throws segfault Don C Programming 60 03-19-2010 05:58 AM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments