gpt4 book ai didi

python - 在Python中转换结构

转载 作者:行者123 更新时间:2023-11-30 23:18:48 29 4
gpt4 key购买 nike

我正在使用 ctypes 从外部数据库读取一些数据。

这个数据是写在struct中的。问题是,收到的数据可能有不同的结果。为了更好地理解:我创建了两个结构:

class BEAM(Structure):
_fields_ = [
('NR', c_ulong),
("NODE1", c_ulong),
("NODE2", c_ulong),
("NP", c_ulong),
("DL", c_float),
("foo", c_ulong),
("foobar", c_ulong),
("bar", c_ulong),
("barfoo", c_ulong)
]

class DUMMY(Structure):
_fields_ = [
('ID', c_ulong),
("NODE1", c_ulong),
("NODE2", c_ulong),
("NP", c_ulong),
("DL", c_ulong),
("foo", c_ulong),
("foobar", c_ulong),
("bar", c_ulong),
("barfoo", c_ulong)
]

这个结构之间的区别在于“DL”中的 u_long 类型...在 DUMMY 中它是 u_long,在 BEAM 中它是 u_float。

读取数据库后,我进入 DL = 1056964624,但在 float 中应该是 0.5

我的问题是如何将 DUMMY 转换到 BEAM 中。

我已经尝试过了BEAMRecord = cast(Record, POINTER(BEAMRecord)) 但有一个错误称为类型错误:必须是 ctypes 类型

这是我的代码:

'''
Structure for DataLength
'''
class Len(Structure):
_fields_ = [
('buffer', c_int)
]


SLNRecord = element.SLN()
BEAMRecord = element.BEAM()

Record = element.DUMMY()
RecLen = Len()

sofistik = cdll.LoadLibrary("cdb_w30_x64.dll")

py_sof_cdb_init = sofistik.sof_cdb_init
py_sof_cdb_close = sofistik.sof_cdb_close
py_sof_cdb_get = sofistik.sof_cdb_get

py_sof_cdb_get.restype = c_int

Index = py_sof_cdb_init("system.cdb", 99)

pos = c_int(0)

while True:
RecLen.buffer = sizeof(Record)

ie = py_sof_cdb_get(Index, 100, 0, byref(Record), byref(RecLen), pos)

pos.value += 1

if ie > 1:
break

if Record.ID > 0:
BEAMRecord = cast(Record, POINTER(BEAMRecord))
print BEAMRecord

py_sof_cdb_close(0)

exit()

感谢您的帮助

<小时/>

解决方案:

通过阅读this thread我修改了@临时先生的问题

我创建了一个 BEAMRecordPointer = POINTER(element.BEAM)BEAMRecord = cast(Record, POINTER(BEAMRecord)) 我重写为 BAR = cast( byref(Record), BEAMRecordPointer).contents所以解决方案如下所示

if Record.ID > 0:
BAR = cast(byref(Record), BEAMRecordPointer).contents
print BAR

我做错了吗?

<小时/>

更新1

@eryksun 对cast() 函数有非常好的截图。谢谢。

最佳答案

您可以将结构加载到Union中,然后按照您需要的方式访问它:

from ctypes import *

class BEAM(Structure):
_fields_ = [('NR', c_ulong),
("NODE1", c_ulong),
("NODE2", c_ulong),
("NP", c_ulong),
("DL", c_float),
("foo", c_ulong),
("foobar", c_ulong),
("bar", c_ulong),
("barfoo", c_ulong)]

class DUMMY(Structure):
_fields_ = [('ID', c_ulong),
("NODE1", c_ulong),
("NODE2", c_ulong),
("NP", c_ulong),
("DL", c_ulong),
("foo", c_ulong),
("foobar", c_ulong),
("bar", c_ulong),
("barfoo", c_ulong)]

class Both(Union):
_fields_ = [('Beam',BEAM),('Dummy',DUMMY)]

x = Both()
x.Dummy.DL = 1056964624
print(x.Beam.DL)

输出:

0.5000009536743164

或者更简单地说:

from ctypes import *

class DL(Union):
_fields_ = [('DUMMY',c_ulong),('BEAM',c_float)]

class Hybrid(Structure):
_fields_ = [('NR', c_ulong),
("NODE1", c_ulong),
("NODE2", c_ulong),
("NP", c_ulong),
("DL", DL),
("foo", c_ulong),
("foobar", c_ulong),
("bar", c_ulong),
("barfoo", c_ulong)]

x = Hybrid()
x.DL.DUMMY = 1056964624
print(x.DL.BEAM)

(相同的输出)

关于python - 在Python中转换结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26482880/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com