How to compute CRC of "05 08" to get "9e 17"?

Using the function below to compute the crc of unsigned char tmp[] = {0x05, 0x08}, I got "b6 bb". But someone told me the correct result is "9e 17". Who is right? Comments on this issue will be appreciated very much. Thanks in advance.

1
2
3
4
5
unsigned char tmp[] = {0x05, 0x08};
unsigned short polinomial = 0x0589;
unsigned short crc = checksum_calculateCRC16(tmp, 2,polinomial);
memcpy( tmp, &crc, 2 );
return crc;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
unsigned short checksum_calculateCRC16(unsigned char data[], unsigned short length, unsigned short polynomial)
{
	unsigned char j, xor_flag, bit_mask, byte; // bit counter, XOR flag, bit mask, current byte
	unsigned short i; // byte counter
	unsigned short total_length = length + 2; // original length + two 0x00 bytes
	unsigned short remainder = 0xFFFF; // CRC remainder

	xor_flag = 0x00;

	// Process all bytes
	for(i = 0; i < total_length; i++)
	{
		// Set bit mask for next byte
		bit_mask = 0x80;

		// Add two bytes with 0x00 after original data
		byte = 0x00;
		if(i < length)
		{
			byte = data[i];
		}

		// Process all bits
		for(j = 0; j < 8; j++)
		{
			// If left-most bit is a 1
			if((remainder & 0x8000) == 0x8000)
			{
				// Set XOR flag
				xor_flag = 0x01;
			}

			// Right-shift remainder
			remainder = remainder << 1;

			// If current bit is a 1
			if((byte & bit_mask) == bit_mask)
			{
				// Insert a 1 at right-most position of remainder
				remainder++;
			}

			// If XOR flag is set
			if(xor_flag == 0x01)
			{
				// XOR remainder with polynomial
				remainder ^= polynomial;
				// Clear XOR flag
				xor_flag = 0x00;
			}

			// Shift mask to process next bit
			bit_mask = bit_mask >> 1;
		}
	}

	// Return remainder
	return remainder;
}

There are many online CRC calculators such as this one: http://www.lammertbies.nl/comm/info/crc-calculation.html

Google is your friend :)
http://lmgtfy.com/?q=online+crc+calculator

Using sites like those will allow you to check against your code.
Last edited on
Topic archived. No new replies allowed.