如何将c中的以下结构转换为python?

2024-06-16 10:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将ieee80211_hdr(如下)转换为python结构,该结构可以解码bytearray并存储变量

谢谢

struct ieee80211_hdr {
    unsigned char frame_control[2];
    unsigned char duration_id[2];
    unsigned char addr1[6];
    unsigned char addr2[6];
    unsigned char addr3[6];
    unsigned char seq_ctrl[2];
    unsigned char addr4[6];

}


Tags: idhdr解码结构framestructcontrolduration
2条回答

python中有一个struct类https://docs.python.org/3/library/struct.html

This module performs conversions between Python values and C structs represented as Python bytes objects.

您还可以查看包dpkthttps://dpkt.readthedocs.io/en/latest/index.html

请使用StackOverflow的功能将代码嵌入为文本,而不是使用图像

如何使用struct转换为class,如下所示:

class ieee80211_hrd:
    def __init__(self, frame_control, duration_id, addr1, addr2, addr3, seq_ctrl, addr4):
        self.frame_control = frame_control
        self.duration_id = duration_id
        self.addr1 = addr1
        self.addr2 = addr2
        self.addr3 = addr3
        self.seq_ctrl = seq_ctrl
        self.addr4 = addr4

这将创建一个可以像这样使用的对象

thing = ieee80211_hrd("ab", "cd", "efghij", "klmnop", "qrstuv", "wx", "yz1234")
print(thing.addr3)
# and so on, where `thing.addr3`, etc. are persistent variables in the class instance `thing`

相关问题 更多 >