CRC8 Python Code, How to convert a list into CRC8 values?

191 views Asked by At

There are errors(syntax and many more), when i convert a list into crc8 hash values. I want to convert a list into CRC8 hash values. Please have a look

import crc8
hash = crc8.crc8()
hash.update(b'123')
assert hash.hexdigest() == 'c0'
assert hash.digest() == b'\xc0'  

This is the sample code to convert string into crc8 hash values. I want to convert a list into hash values. Each item in the list needs to be converted into hash values.

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'  

Output of conversion should look like this

["0x66","0xBF","0x1A"]

1

There are 1 answers

2
SIGHUP On

The crc8 class requires bytes - not strings. Therefore:

from crc8 import crc8

lst = [b'yas123', b'nihao123', b'hello123']
output = [hex(ord(crc8(e).digest())) for e in lst]
print(output)

Output:

['0xb3', '0x1a', '0xbf']

Note:

Not sure why the expected output is as stated in the question

Alternative implementation:

from crc8 import crc8

lst = ['yas123', 'nihao123', 'hello123']
output = [hex(ord(crc8(e.encode()).digest())) for e in lst]
print(output)