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

ctypes: Get full contents of character array

Hello!

I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however, ctypes seems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.

I ended up having to define a dummy struct
class dummystruct(Structure):
_fields_ = []

and declare array as:
("array", dummystruct)

then use string_at(byref(struct.array), 12).

Is this really the best way of doing it? Is there no better way to
work around ctypes 'guess what you want' behaviour?

Thanks in advance,
Rodrigo
Sep 12 '08 #1
2 10036
On Sep 12, 6:38*pm, overdrig...@gmail.com wrote:
Hello!

I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however, ctypes seems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.

I ended up having to define a dummy struct
class dummystruct(Structure):
* * _fields_ = []

and declare array as:
("array", dummystruct)

then use string_at(byref(struct.array), 12).

Is this really the best way of doing it? Is there no better way to
work around ctypes 'guess what you want' behaviour?

Thanks in advance,
Rodrigo
Rodrigo,

If you have the option to change your declaration to c_byte* 12, you
have more options. This example prints the null character you wanted.

from ctypes import *
import struct
class S( Structure ):
_fields_= [
( 'array', c_byte* 12 )
]
s= S()

#initialize
struct.pack_into( '7s', s.array, 0, 'abc\x00def' )

#prototype and call PyString_FromStringAndSize
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_size_t )
PyString_FromStringAndSize= prototype( ( "PyString_FromStringAndSize",
pythonapi ) )
x= PyString_FromStringAndSize( s.array, 12 )
print repr( x )

#prototype and call PyString_FromString for contrast
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ) )
PyString_FromString= prototype( ( "PyString_FromString", pythonapi ) )
x= PyString_FromString( s.array )
print repr( x )

/Output:

'abc\x00def\x00\x00\x00\x00\x00'
'abc'
Sep 13 '08 #2
On Sep 13, 6:45*am, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:
On Sep 12, 6:38*pm, overdrig...@gmail.com wrote:


Hello!
I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however,ctypesseems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.
I ended up having to define a dummy struct
class dummystruct(Structure):
* * _fields_ = []
and declare array as:
("array", dummystruct)
then use string_at(byref(struct.array), 12).
Is this really the best way of doing it? Is there no better way to
work aroundctypes'guess what you want' behaviour?
Thanks in advance,
Rodrigo

Rodrigo,

If you have the option to change your declaration to c_byte* 12, you
have more options. *This example prints the null character you wanted.

fromctypesimport *
import struct
class S( Structure ):
* * _fields_= [
* * * * ( 'array', c_byte* 12 )
* * ]
s= S()

#initialize
struct.pack_into( '7s', s.array, 0, 'abc\x00def' )

#prototype and call PyString_FromStringAndSize
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_size_t )
PyString_FromStringAndSize= prototype( ( "PyString_FromStringAndSize",
pythonapi ) )
x= PyString_FromStringAndSize( s.array, 12 )
print repr( x )

#prototype and call PyString_FromString for contrast
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ) )
PyString_FromString= prototype( ( "PyString_FromString", pythonapi ) )
x= PyString_FromString( s.array )
print repr( x )

/Output:

'abc\x00def\x00\x00\x00\x00\x00'
'abc'
Great - and thank you for replying so fast! I found that [chr(x) for x
in struct.array] with array as a c_byte * 12 did what I needed.
Sep 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
0
by: Sanghyeon Seo | last post by:
ctypes is a popular CPython extension planned for inclusion in Python 2.5. It is a foreign function interface library. Homepage: http://starship.python.net/crew/theller/ctypes/ Documentation:...
0
by: chris.atlee | last post by:
Hello, I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the script below), you're passed in a pam_response** pointer. You're supposed to allocate...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
1
by: moreati | last post by:
Recently I discovered the re module doesn't support POSIX character classes: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for...
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
1
by: dudeja.rajat | last post by:
Hi, I'm using the CTYPES module of python to load a dll. My dll contains a Get_Version function as: long __stdcall af1xEvdoRDll_GetVersion(long version); This function accepts a long array of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.