jorrebor
jorrebor

Reputation: 2232

How to convert CRC 16 from hexidecimal serie in Python

I have a serie of hexidecimal bytes:

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

I need to calculate the CRC 16 of this. An online calcultor gives me:

0x81 0x53 //correct

I use the crcmod python module as follows:

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

print crc16('123456789') # works well

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

How can i represent this hexidecimal serie as an ascii string (which the function requieres )

Thank you!

Upvotes: 1

Views: 3322

Answers (2)

glglgl
glglgl

Reputation: 91017

If you can freely edit your stuff, Marcus is right, otherwise (e.g. if you have your bytes already somewhere in your program), do

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

Upvotes: 1

Marcus
Marcus

Reputation: 6839

use '\x81\x12\xC0\x00\x01\x05'

Upvotes: 1

Related Questions