By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

C type buffer copy

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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;-)

"jo*@gmail.com" wrote:

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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
"jo*@gmail.com" <do*****@gmail.com> writes:
What's the best way to deal with this in python?


I can't tell what you're trying to do. If you want an array
containing the numbers 0,1,...,n-1, just say
buf = range(n)
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.