Using the ctypes module I can easily import a POINTER(c_char) or a c_char_p type into python, but neither of these provides a way to end up with a python string that contains zero value bytes.
c_char_p is zero terminated, meaning that a char * array from C is terminated at the first zero value.
POINTER(c_char) is the recommended way of importing binary data that can have 0 values, but there doesn't seem to be a way to directly convert this into a python string.
I can do this:
pixels = clibblah.get_pixels()
a = ""
for i in range(0, clibblah.get_pixel_length()):
a += pixels[i]
As you said, use a
POINTER(c_char) to get a pointer to the array of binary data. To put that together into a string, you can just take a slice of it, since array indexing works as expected with ctypes pointers:
clibblah = ctypes.cdll.LoadLibrary('clibblah.dylib') get_pixels = clibblah.get_pixels get_pixels.restype = ctypes.POINTER(ctypes.c_char) pixels = get_pixels() num_pixels = clibblah.get_pixel_length() # Slice the ctypes array into a Python string a = pixels[:num_pixels]