Author Topic: Can someone help me with CRC16 calculations?  (Read 2542 times)

LJH

  • *
  • Posts: 132
    • View Profile
Can someone help me with CRC16 calculations?
« on: 2009-04-06 07:13:37 »
Can someone please explain, in basic terms, how a CRC16 calculation works, or point out what I'm doing wrong?

I need to know how to do it with any given polynomial and "initial value" (Hex Workshop has a setting for this and I don't understand how it even factors into a calculation)...

I understand some technical related stuff (XOR, bits, bytes, endianness), but I can't seem to get a calculation to work correctly.

Using 0x8005 (yes, I know technically this means 0x18005, I haven't forgotten this) as a polynomial on the string "123456789" (without quotes, and as the characters, not the number), working by hand I get DFD8 and by my program I get 580C, when it should be (both from a website I found and Hex Workshop's calculation) BB3D. Obviously I'm doing something wrong along the line... here's how I'm doing it:

By hand:
1) Convert the entire string to binary (I am 100% sure I'm doing this correctly), I've tried both without and with adding two empty bytes to the end (without them, I got DAA6)
2) Line the polynomial (also converted in binary, and remembering to add the extra x^16 bit) up with the first 1 in the string
3) XOR it with the bits above it in the string (it's possible I made a mistake here but I'm fairly sure I didn't)
4) The bits that were not lined with the polynomial, just carry them down (eg: if string was 1110001101, and polynomial only covered first four bits, would carry the 001101 down and append it to the result)
5) Repeat until the string is shorter than the polynomial

By program (I'm using a form of BASIC, so if there's terms here with different meanings in different languages, assume the BASIC meaning)
1) Convert the string and polynomial to binary, in arrays with one array element per byte (eg: if the first character was 3C in hex, which is 00111100 in binary, the first 8 elements of the array would be 0, 0, 1, 1, 1, 1, 0, 0) - yes, I did treat the polynomial as a number and not a string, and once again, I remembered to add the x^16 bit, I also added the two empty bytes to the end of the string, I know this part is done right
2) Look through the string's array for the first 1, then from this point, XOR each element with the polynomial's value in the same position
3) Repeat step 2 until the 17th to last element is 0
4) Convert back to decimal/hex (I know this is done right)

Any advice would be great, thanks.

nfitc1

  • *
  • Posts: 3011
  • I just don't know what went wrong.
    • View Profile
    • WM/PrC Blog
Re: Can someone help me with CRC16 calculations?
« Reply #1 on: 2009-04-07 20:13:03 »
It depends on the method used to generate the CRC. Most of them use table look-ups. I just found this: http://www.programmersresource.com/forum/showthread.php?t=462

The code is on the third post. Good luck.