我是靠谱客的博主 飞快黄蜂,这篇文章主要介绍python const char_[Python]关于ctypes使用char*指针与bytes相互转换的问题,现在分享给大家,希望可以做个参考。

最近研究人脸识别,需要用python调用so动态库,涉及到c/c++中的指针字符串转Python的bytes对象的问题。

按照ctypes的文档,直观方式是先创建对应的类型数组,再将指针取地址一一赋值:

from ctypes import *

p=(c_char * 10)()

for i in range(10):

p[i] = i

b=bytes(bytearray(p))

print(b)

from ctypes import *

p=(c_char * 10)()

for i in range(10):

p[i] = i

b=bytes(bytearray(p))

print(b)

搜寻了各种资料,都未能找到更好的。。。直到ctypes.string_at

_string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)

def string_at(ptr, size=-1):

"""string_at(addr[, size]) -> string

Return the string at addr."""

return _string_at(ptr, size)

_string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)

def string_at(ptr, size=-1):

"""strin

最后

以上就是飞快黄蜂最近收集整理的关于python const char_[Python]关于ctypes使用char*指针与bytes相互转换的问题的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部