Python中,将一段内存转换成一个数组

from ctypes import *
import struct
class MyStruct(Structure):
_fields_ = [("a", c_int),
     ("b", c_int)]
fmt = "%i"%60+"s"                                 # 格式化串定义
buf = struct.pack(fmt, "0")                     # 申请格式化大小的空间
sset = cast(buf, POINTER(MyStruct))   # 将缓冲区转换成数组
nCnt = 60/sizeof(MyStruct)
nIdx = 0
while nIdx < nCnt:
    print sset[nIdx].a
    nIdx = nIdx + 1

你可能感兴趣的:(Python中,将一段内存转换成一个数组)