Re: 2.6 - 3.x bytearray from c_char_ p

Thomas Stover <[email protected]> Tue, 17 May 2011 15:26:08 -0500
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
> From: "Jeff C. Britton" <[email protected]>
...
> 
> I tested the following and it worked.
> 
> from ctypes import *
> import ctypes
> import struct
> 
> # I created a .dll named callback.dll for testing
> dll = cdll.callback
> 
> def Callback(pointer, length):
>     s = pointer[:length]
>     print map(ord, s)
>     # or
>     print struct.unpack('B'*length, s)
>     return 0
>     
...

Just now getting back to this. 

1) On the struct.unpack(), I get

struct.error: unpack requires a string argument of length 47

any ideas?

2) The other part did work fine, but I'm still scratching my head. What
I'm learning from your example are the map() and ord() functions.

From the python docs:


map(function, iterable, ...)¶
    Apply function to every item of iterable and return a list of the
results. If additional iterable arguments are passed, function must take
that many arguments and is applied to the items from all iterables in
parallel. If one iterable is shorter than another it is assumed to be
extended with None items. If function is None, the identity function is
assumed; if there are multiple arguments, map() returns a list consisting
of tuples containing the corresponding items from all iterables (a kind of
transpose operation). The iterable arguments may be a sequence or any
iterable object; the result is always a list.


ord(c)¶
    Given a string of length one, return an integer representing the
Unicode code point of the character when the argument is a unicode object,
or the value of the byte when the argument is an 8-bit string. For example,
ord('a') returns the integer 97, ord(u'\u2020') returns 8224. This is the
inverse of chr() for 8-bit strings and of unichr() for unicode objects. If
a unicode argument is given and Python was built with UCS2 Unicode, then
the character’s code point must be in the range [0..65535] inclusive;
otherwise the string length is two, and a TypeError will be raised.


Since in this time I didn't get any errors resulting from "malformed
encoding", I assume the string auto-magically gets created as an "8 bit"
string. How does one even check what type of string they have? Is there a
way to ensure this happens every time?

Thanks for at least getting me this far.

-- 
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