gpt4 book ai didi

python - 从 ctypes 的 c_char_p 中检索字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:45 24 4
gpt4 key购买 nike

在 Python 中,我有一个 ctypes.c_char_p 的字符串,'Hello, world'。似乎 python 自动将它编码为一个数字(从我的 ipython 控制台看下面)。如何从 ctypes.c_char_p 对象中检索字符串,即“Hello, world”?

In [45]: from ctypes import c_char_p

In [46]: c_char_p("Hello, World")
Out[46]: c_char_p(4508383228)

最佳答案

正如评论中已经提到的,尽管可能不像某些人要求的那样明确,.value 属性将 ctypes.c_char_p 转换为 Python 字符串。

此代码将 Python 字符串往返到 ctypes.c_char_p,然后返回到 Python 字符串:

>>> p = c_char_p("Hello, World")
>>> p
c_char_p(140323010068388)
>>> type(p)
<class 'ctypes.c_char_p'>
>>> s = p.value
>>> s
'Hello, World'
>>> type(s)
<type 'str'>

关于python - 从 ctypes 的 c_char_p 中检索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com