如何将 _ctypes.pycarraytype 转换为 bytearray?

问题描述 投票:0回答:1

我知道如何在 ctype 实例中存储字节数组

import ctypes

c_char_Arr = ctypes.c_char * 4

c_char_Arr.from_buffer(bytearray([0] * 4))

如何从 ctype 实例获取字节数组?

我在这里也可能做错了什么,因为我与 ctype 共享

bytearray
缓冲区
c_char

python ctypes
1个回答
0
投票

保留参考。

.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
© www.soinside.com 2019 - 2024. All rights reserved.