C type buffer copy

J

joe

Hello,

How does Python deal with C type memory buffers. Does Python return
everything as an object irrespective of the data type?

Here's what i am trying to achieve?

testCode(unsigned char buf, unsigned long len)
{
unsigned long data=0x0;
while (len--)
{
*buf++ = (unsigned char)data++
}
....
}

What's the best way to deal with this in python?

-Joe
 
I

Ivan Van Laningham

Hi All--

def testCode(data):
buf=data[:]
# and I hope you're going to do something with buf,
# because otherwise this function's a bit of a waste;-)

Hello,

How does Python deal with C type memory buffers. Does Python return
everything as an object irrespective of the data type?

Here's what i am trying to achieve?

testCode(unsigned char buf, unsigned long len)
{
unsigned long data=0x0;
while (len--)
{
*buf++ = (unsigned char)data++
}
....
}

What's the best way to deal with this in python?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
N

Neil Hodgson

Joe:
testCode(unsigned char buf, unsigned long len)
{
unsigned long data=0x0;
while (len--)
{
*buf++ = (unsigned char)data++

This C code will crash since buf is declared as an unsigned char, not
an unsigned char*. Stop thinking in terms of translating low level C
because Python is a higher level language and does not have machine
address pointers. You should be implementing the intent of your design
which we can't determine from tiny incorrect snippets. For example, we
can't determine if the intent is to create a new string, translation:
''.join([chr(x) for x in range(len)]), or if you are modifying an
existing sequence or inserting into a sequence. It is also impossible to
tell if you should be using a list, array or string for this job.

Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top