Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中对原始二进制数据使用位运算符进行CRC检查?_Python_Bit Manipulation_Crc - Fatal编程技术网

如何在Python中对原始二进制数据使用位运算符进行CRC检查?

如何在Python中对原始二进制数据使用位运算符进行CRC检查?,python,bit-manipulation,crc,Python,Bit Manipulation,Crc,这是我第一次在Python中使用bitewise运算符和原始二进制数据,我正在努力编写文档中定义的CRC检查代码 文档定义了CRC检入伪码,如下所示: GENERATOR = 1111111111111010000001001 MSG = binary("8D4840D6202CC371C32CE0576098") # total 112 bits FOR i FROM 0 TO 88: # 112 - 24 parity bits

这是我第一次在Python中使用bitewise运算符和原始二进制数据,我正在努力编写文档中定义的CRC检查代码

文档定义了CRC检入伪码,如下所示:

GENERATOR = 1111111111111010000001001

MSG = binary("8D4840D6202CC371C32CE0576098")  # total 112 bits

FOR i FROM 0 TO 88:                           # 112 - 24 parity bits
  if MSG[i] is 1:
    MSG[i:i+24] = MSG[i:i+24] ^ GENERATOR

CRC = MSG[-24:]                               # last 24 bits

IF CRC not 0:
  MSG is corrupted
到目前为止,我已经编写了以下代码:

adsb_hex = "8D4840D6202CC371C32CE0576098"
adsb_dec = int(adsb_hex, 16)
adsb_bin = bin(adsb_dec)[2:].zfill(8)

generator = 0b1111111111111010000001001

adsb_bin_list = [int((adsb_dec >> bit) & 1) for bit in range(112 - 1, -1, -1)]

check = []
for i in range(88):
    curr_bit = adsb_bin_list[i]
    if curr_bit is 1:
        check[i:i+24] = int(adsb_bin_list[i:i+24]) ^ generator

crc = check[-24:]
我不知道正确的蟒蛇方法:

MSG[i:i+24] = MSG[i:i+24] ^ GENERATOR

CRC = MSG[-24:]

我如何才能正确地使用python方法呢?

这里有一种更适合python的方法,尽管它放弃了直接位算法

msg_hex = "8D4840D6202CC371C32CE0576098"
msg_dec = int(msg_hex, 16)
msg_bin = list(bin(msg_dec)[2:].zfill(8))

generator = "1111111111111010000001001"

for i in range(88):
    if msg_bin[i] == "1":
        msg_bin[i:i+24] = ["1" if a != b else "0" for a, b in zip(msg_bin[i:i+24], generator)]

if int("".join(msg_bin[-24:])):
    print("corrupted")
如果!=b如果你愿意的话,用
“01”[a!=b]
替换“0”,但我认为前者更像python


我无法验证这个解决方案是否正确,因为您的代码没有编译,并且没有提到给定的生成器和消息应该生成什么。但是,我确实怀疑问题陈述中有错误,因为它正在检查最后24位,但生成器包含25位。

如果您可以使用库,我建议搜索您最喜欢的位字符串/位向量库。例如,使用您可以编写

from BitVector import BitVector

generator = BitVector(bitstring='1111111111111010000001001')
crc_length = len(generator) - 1

def compute_crc(message : BitVector) -> BitVector:
    content_length = len(message) - crc_length
    # to encode a message, you pad it with 0
    # to decode, you pass in the message with the crc appended
    assert content_length >= 0
    # operate on a copy of the data
    message = message.deep_copy()
    for i in range(content_length):
        if message[i]:
            message[i:i + len(generator)] ^= generator
    return message[-crc_length:]

if __name__ == "__main__":
    adsb_hex = "8D4840D6202CC371C32CE0576098"
    adsb_crc = compute_crc(BitVector(hexstring = adsb_hex))

我想您可能需要将语句中的
int
转换为
str
check[i:i+24]=int(adsb\u bin\u list[i:i+24])^generator
工作!用纯Python替换位向量真的很难吗?将尝试这样做,因为在此处添加LIB需要批准。至少我有一个可以工作的原型@AlexV位向量库,据我所知,这只是一个源文件,您可以在中找到,我没有查看许可证。最重要的部分是(1)位索引/切片(2)支持len()(3)构造函数(4)逻辑运算符,即使对于切片基本上也会权衡您的选择,但为了可读性,导入位向量类或自己编写位向量类是必须的