当我将列表转换为crc8哈希值时,会出现错误(语法等等)。我希望将列表转换为CRC8哈希值。请看一看
import crc8
hash = crc8.crc8()
hash.update(b'123')
assert hash.hexdigest() == 'c0'
assert hash.digest() == b'\xc0' 这是将字符串转换为crc8哈希值的示例代码。我想将列表转换为哈希值。列表中的每个项都需要转换为散列值。
import crc8
list = b["ya123","hello123","nihao123"]
for i in list:
hash = crc8.crc8()
hash.update(i)
assert hash.hexdigest() == 'c0'
assert hash.digest() == b'\xc0' 转换的输出应该如下所示
["0x66","0xBF","0x1A"]发布于 2022-08-30 06:58:42
crc8类需要字节,而不是字符串。因此:
from crc8 import crc8
lst = [b'yas123', b'nihao123', b'hello123']
output = [hex(ord(crc8(e).digest())) for e in lst]
print(output)输出:
['0xb3', '0x1a', '0xbf']注:
不清楚为什么预期产出如问题中所述
替代实现:
from crc8 import crc8
lst = ['yas123', 'nihao123', 'hello123']
output = [hex(ord(crc8(e.encode()).digest())) for e in lst]
print(output)https://stackoverflow.com/questions/73537638
复制相似问题