我正在尝试将 C 风格的 const char[] 字符串指针(从 DLL 返回)转换为 Python 兼容的字符串类型。但是当 Python27 执行时:
import ctypes
charPtr = ctypes.cast( "HiThere", ctypes.c_char_p )
print( "charPtr = ", charPtr )
我们得到:charPtr = c_char_p('HiThere')
也许有些事情没有被正确评估。 我的问题是:
- 应该如何将这个 charPtr 转换回 Python 兼容的、可打印的字符串?
- 刚才提到的cast操作是不是在做它应该做的事情?
最佳答案
ctypes.cast() 用于将一个 ctype 实例转换为另一种 ctype 数据类型。 您不需要将其转换为 python 字符串。 只需使用“.value”在 python 字符串中获取它。
>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> print c_s.value
Hello, World
更多信息 here
关于python - 从 DLL 返回的 char[] 转换为 Python 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8875185/