Re: 2.6 - 3.x bytearray from c_char_p
"Jeff C. Britton" <[email protected]> Tue, 17 May 2011 19:20:03 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
It looks like you have solved your problem. I am not sure if you still have questions.
You can create a Unicode string with
u = u'hello'
and then check the type
>>>type(u)
<type 'unicode'>
As opposed to
s = 'hello'
>>>type(s)
<type 'str'>
A POINTER(c_char) will represent an array of 1 byte character values.
I just realized a better solution.
A POINTER(c_byte) will represent an array of 1 byte integers.
This will then get you what you want without needing to use "map(ord, )" or struct.unpack.
If the description of map() was too confusing, here are some examples:
def sqrt(x):
return x*x
>>>map(sqrt, [1,2,3,4])
[1, 4, 9, 16]
def add(x,y):
return x + y
>>>map(add, [1,2,3], [4,5,6])
[5, 7, 9]
ie. [1+4, 2+5, 3+6]
You can also do list comprehensions
>>>[sqrt(x) for x in [1,2,3,4]]
[1, 4, 9, 16]
>>>[add(x,y) for (x,y) in zip([1,2,3],[4,5,6])]
[5, 7, 9]
--Jeff
-----Original Message-----
From: Thomas Stover [mailto:[email protected]]
Sent: Tuesday, May 17, 2011 4:06 PM
To: [email protected]
Subject: Re: [ctypes-users]2.6 - 3.x bytearray from c_char_p
Turns out it actually wasn't working. It stopped on bytes of value 0
thinking it was a NULL termination.
I found in the docs:
class ctypes.c_char_p¶
Represents the C char * datatype when it points to a zero-terminated
string. For a general character pointer that may also point to binary data,
POINTER(c_char) must be used. The constructor accepts an integer address,
or a string.
So the callback argument is of type POINTER(c_char) instead of c_char_p.
Given a parameter, Pointer, of such a type, to get a usable string of
"Length" size, use the notation Jeff gave:
def Callback(Pointer, Length):
Usable = Pointer[:Length]
--
www.thomasstover.com
FLYNN LIVES!
------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay