简体   繁体   English

从python中的c dll获取char数组作为字符串

[英]Getting char array from c dll in python as string

I am a beginner with ctypes and I can not figure out how to turn char array into a string in python. 我是ctypes的初学者,我不知道如何在python中将char数组转换为字符串。 I have started using : 我已经开始使用:

create_string_buffer(bytes(var, 'utf8')) to populate an array, but had no luck with getting it back as a string using "byref()". create_string_buffer(bytes(var, 'utf8'))填充数组,但使用“ byref()”将其作为字符串返回时并没有运气。

If someone has any idea you would be a life saver. 如果有人有任何想法,您将成为救生员。 Thanks in advance! 提前致谢!

ctypes page: [Python]: ctypes - A foreign function library for Python . ctypes页面: [Python]:ctypes-Python的外部函数库
It's hard to tell without seeing some code, but here's an example: 很难看到一些代码,但这是一个示例:

 >>> import sys >>> import ctypes >>> "Python {:s} on {:s}".format(sys.version, sys.platform) 'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \\n[GCC 5.4.0 20160609] on linux' >>> src = "some dummy Python string" >>> src_charp = ctypes.create_string_buffer(src.encode()) >>> dst_initial = b"Just " >>> dst_charp = ctypes.create_string_buffer(len(dst_initial) + len(src) + 1) >>> dst_charp <ctypes.c_char_Array_25 object at 0x7fe6c41209d8> >>> dst_charp.value b'' >>> for idx, c in enumerate(dst_initial): ... dst_charp[idx] = c ... >>> dst_charp.value b'Just ' >>> ctypes.CDLL(None).strcat(dst_charp, src_charp) 32414128 >>> dst_charp <ctypes.c_char_Array_25 object at 0x7fe6c41209d8> >>> dst_charp.value b'Just some dummy Python string' >>> dst = dst_charp.value.decode() >>> dst 'Just some dummy Python string' 

Notes : 注意事项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM