如何将具有0值字节的char *转换为python字符串? [英] How do you convert a char * with 0-value bytes into a python string?

查看:98
本文介绍了如何将具有0值字节的char *转换为python字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ctypes模块,我可以轻松地将POINTER(c_char)或c_char_p类型导入python,但是这些都不提供以包含零值字节的python字符串结尾的方法。



c_char_p终止为零,这意味着来自C的char *数组终止于第一个零值。



POINTER(c_char)是建议的方法是导入可以具有0个值的二进制数据,但是似乎没有一种方法可以直接将其转换为python字符串。



  pixels = clibblah.get_pixels()
a =
for i in range(0, clibblah.get_pixel_length()):
a + =像素[i]

...但是这个1)看起来不是很pythony,而2)会花很多时间(在我的mac上转换640x480像素数据块大约需要2秒)。



在堆栈溢出中看到一堆与此有关的问题,但真是胆怯,如果我能看到一个不是两个人都去的那个你为什么需要这样做?或 c_char_p会做您想要的事情(如上所述,它不会这样做)。



我看到的唯一可靠的建议是使用c api PyString_FromStringAndSize,如此处建议的那样:
http: //www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/FAQ.html



看看这有什么帮助,因为afaik是cython功能,而不是python。



对于感兴趣的人,我需要这样做的原因是我正在与panda3d和kinect,kinect c api提供了一个无符号char *值数组,而panda3d api则提供了setPixels()调用,该调用仅将python字符串作为参数。

POINTER(c_char)获取指向二进制数据数组的指针。要将其放到一个字符串中,您可以将其切成薄片,因为数组索引可以按预期与ctypes指针一起工作:

  clibblah = ctypes.cdll.LoadLibrary('clibblah.dylib')
get_pixels = clibblah.get_pixels
get_pixels.restype = ctypes.POINTER(ctypes.c_char)

pixel = get_pixels()
num_pixels = clibblah.get_pixel_length()

#将ctypes数组切成Python字符串
a = pixel [:num_pixels]


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]

...but this 1) doesn't seem very pythony, and 2) takes forever (converting a 640x480 block of pixel data takes about 2 seconds on my mac).

I've seen a bunch of questions regarding this on stack overflow, but darned if I can see one that isn't either people going "why do you need to do that?" or "c_char_p will do what you want" (it doesn't, as I've described above).

The only credible advice I've seen is to use the c api PyString_FromStringAndSize, as recommended here: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/FAQ.html

Can't really see how that helps though, because afaik that's a cython feature, not a python one.

For the interested, the reason I need to do this is I'm working with panda3d and a kinect, and the kinect c api provides an array of unsigned char * values and the panda3d api lovingly provides a setPixels() call that only takes a python string as an argument.

解决方案

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]

这篇关于如何将具有0值字节的char *转换为python字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆