473,394 Members | 1,817 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.

C type buffer copy

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

Similar topics

6
by: tvn007 | last post by:
Hi, I tried to convert string data type to char type so that I can use strtok. I have used c_str(). However, still getting error message when compile . Any help would be appreciate.
11
by: Danny J. Lesandrini | last post by:
Has anyone else noticed this? I searched group archives and found nothing, but it's really starting to irritate me big time. Here's the steps to reproduce the problem: Copy something to the...
9
by: mario.demiguel | last post by:
Hello all, I believe I know the basics of what I need to do, but some clarification would help. I want to open a text file using functions from stdio.h. Then I want to output the text in this...
0
by: Jack | last post by:
I am trying to develop a simple telnet client in MS.NET I have a small wrapper around the System.Net.Sockets.Socket class which takes care of sending and receiving data and handing it off to a...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: Greg Young | last post by:
Ok so I think everyone can agree that creating buffers on the fly in an async socket server is bad ... there is alot of literature available on the problems this will cause with the heap. I am...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
1
by: kellox | last post by:
Does anybody know the difference between the two static methods Buffer.BlockCopy and Array.Copy? It is said that Buffer.BlockCopy is faster than Array.Copy since it only checks boundary and then...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.