0

我已经分配了一个数组并使用 Python ctypes 模块对其进行转换:

    dataC = ctypes.cast(crt.malloc(size), ctypes.POINTER(ctypes.c_ubyte))

为了从 C 库中获取字节数据:

    someClib.getData(handle, dataC)

现在这个数组实际上是一个 C 浮点类型的数组。如何将其转换为 Python 浮点类型数字列表?

4

1 回答 1

5

You can cast to a pointer to float:

floatPtr = ctypes.cast(dataC, ctypes.POINTER(ctypes.c_float))

And then use a list comprehension, for example, to pull out the floats:

floatList = [floatPtr[i] for i in range(arrayLength)]

Now, only you know the value of arrayLength but it seems plausible to me that it is equal to size / ctypes.sizeof(ctypes.c_float).

于 2013-10-11T22:06:38.030 回答