我知道如何在 ctype 实例中存储字节数组
import ctypes
c_char_Arr = ctypes.c_char * 4
c_char_Arr.from_buffer(bytearray([0] * 4))
如何从 ctype 实例获取字节数组?
我在这里也可能做错了什么,因为我与 ctype 共享
bytearray
缓冲区 c_char
保留参考。
.from_buffer()
指的是相同记忆:
>>> import ctypes as ct
>>> c = ct.c_char * 4
>>> b = bytearray([0]*4) # b is the reference
>>> m = c.from_buffer(b) # m refer to the same memory
>>> b
bytearray(b'\x00\x00\x00\x00') # b before
>>> m[1]=5 # change via m
>>> b
bytearray(b'\x00\x05\x00\x00') # b updated