如何在Python中从十六进制序列转换crc16

2024-05-29 00:27:25 发布

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

我有一系列十六进制字节:

0x81 0x12 0xC0 0x00 0x01 0x05

我需要计算这个的crc16。An online calcultor gives我:

^{pr2}$

我使用crcmod python模块如下:

crc16 = crcmod.predefined.mkCrcFun('crc-16')

print crc16('123456789') # works well

print hex(crc16('\x81\x12\xC0\x00\x01\x05')) #EDIT : works aswell!

如何将这个十六进制序列表示为ascii字符串(函数需要它)

谢谢你!在


Tags: 模块an字节onlinecrcprintworkswell
2条回答

如果你可以自由编辑你的东西,Marcus is right,否则(例如,如果你的字节已经在你的程序中的某个地方),那么就做吧

values = [0x81, 0x12, 0xC0, 0x00, 0x01, 0x05]
string = "".join(chr(i) for i in values)
print crc16(string)

使用'\x81\x12\xC0\x00\x01\x05'

相关问题 更多 >

    热门问题