0

I am starting to do a new big project so I started to learn about CRC(Cyclic redundancy check). After I had learned about it, I made one (CRC-16, with polynomial x^16+x^15+x^2+1, that equal to hex 0x8005), But when I checked if I did it fine, I found out it right but not the answer I wanted to get. I am using the website: http://crccalc.com/ In the code, I entered the binary value: 0100010101010011 (equal to 0x4553) and got the result is 1001111111101100 ( equal to 0x9FEC) I found out on the website is the right result but it not what I expected to get(I thought I'd get 0xAD72).

I have two questions:

  1. How do I change my code to support CRC-16/ARC and not CRC-16/BUYPASS?

  2. Why the polynomial x^16+x^15+x^2+1 equal to 0x8005 and not 0xC005? Thanks a lot!

my python code:

    def stXor(a,b):
    if(a==b):
        return "0"
    else:
        return "1"

def stshl(str1,shift):
    temp=""
    temp=str1
    for i in range(shift):
        temp=temp[1:]
    return temp

def get_error(data,padding=True):
    good=data
    lst=["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]
    if(padding):
        good=data+"0"*16
    now="0"

    while len(good)>0:

        now=lst[0]
        lst[0]= stXor(lst[1],now) #x^15
        lst[1]=lst[2]
        lst[2]=lst[3]
        lst[3]=lst[4]
        lst[4]=lst[5]
        lst[5]=lst[6]
        lst[6]=lst[7]
        lst[7]=lst[8]
        lst[8]=lst[9]
        lst[9]=lst[10]
        lst[10]=lst[11]
        lst[11]=lst[12]
        lst[12]=lst[13]
        lst[13]=stXor(lst[14],now)# x^2
        lst[14]=lst[15]
        lst[15]=stXor(good[0],now)
        good=stshl(good,1)


    a=""
    for letter in lst:
        a+=letter


    return a


data="0100010101010011"

print("data entered: "+"\nhex: "+str(hex(int(data, 2)))+"\nBinary: "+data)


a=get_error(data)
print("Result: "+"\nhex: "+str(hex(int(a, 2)))+"\nBinary: "+a)

printed output:

data entered:

hex: 0x4553

Binary: 0100010101010011

Result:

hex: 0x9fec

Binary: 1001111111101100

4
  • Where did 0xC003 come from? Commented Dec 23, 2017 at 18:06
  • I'm sorry. I fixed it to 0xC005 Commented Dec 23, 2017 at 22:15
  • Ok, then where did 0xC005 come from? Commented Dec 23, 2017 at 22:42
  • Before your explain i thought the polynomial x^16+x^15+x^2+1 equal to 1100000000000101 (0xC005) and not to 11000000000000101 (0x18005) Commented Dec 24, 2017 at 23:43

2 Answers 2

4

CRC-16/ARC reverses the bits in the calculation as compared to CRC-16/BUYPASS. That is the only difference between the two. So just reverse the register and the input bits.

x16+x15+x2+1 is represented in binary with the locations of the exponents set to 1, so 11000000000000101, or 0x18005. In the calculation, the highest power defines the length of the CRC (16 in this case), and this bit is dropped giving 0x8005.

A CRC would never be implemented that way. If you want to learn about how CRC's are defined and how to implement them, read Ross Williams' excellent tutorial on CRCs. You can find the definitions of many CRC's in Greg Cook's catalog. crcany will generate C code for any CRC.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Your answer helps me a lot!
0

Why not use one of the libraries available to calculate CRC-16? Such as https://pypi.python.org/pypi/crc16/0.1.1

1 Comment

I want to know how it work and make it my self because i will need to make it in other languge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.