4

I'm having problems to make a code in Python 3.4 using the CRCMOD library to get the CCITT CRC16 check.

Thats my string:

a731986b1500087f9206e82e3829fe8bcffed5555efd00a100980000010000000100000009010013bb1d001e287107009b3000000300000088330000f427500077026309

The spected crc value is 1d7f

My code:

import crcmod

crc16 = crcmod.mkCrcFun(0x11021, 0x1d0f, False, 0x0000)

hex(crc16(b'a731986b1500087f9206e82e3829fe8bcffed5555efd00a100980000010000000100000009010013bb1d001e287107009b3000000300000088330000f427500077026309'))

It returns: 7d67

What am I doing wrong?

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
SnowBG
  • 89
  • 1
  • 2
  • 12
  • Are you sure the initial string isn't just a hex representation of binary data ? (i.e. you'll have to convert the hex to binary before calculating the crc) – nos Jun 16 '14 at 13:45
  • Yes, man. I saw it now... have you a tip for me with how can I convert it into HEX??? I try hex('ba7.....) but it doesn't work. – SnowBG Jun 16 '14 at 14:19
  • 1
    `import binascii` `binascii.a2b_hex('a731 ....')` – nos Jun 16 '14 at 15:04

1 Answers1

7

You first need to convert the data from its hex representation to binary. You also need to use the correct CRC algorithm, which I think is "xmodem" - crcmod.mkCrcFun(0x11021, 0x0000, False, 0x0000)

import crcmod.predefined
from binascii import unhexlify

s = unhexlify('a731986b1500087f9206e82e3829fe8bcffed5555efd00a100980000010000000100000009010013bb1d001e287107009b3000000300000088330000f427500077026309')

crc16 = crcmod.predefined.Crc('xmodem')
crc16.update(s)
print crc16.hexdigest()

Outputs 7F1D (which is what you expected but with the bytes reversed)

mhawke
  • 84,695
  • 9
  • 117
  • 138