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.

Qt Code:
  1. unsigned char tmp[] = {0x05, 0x08};
  2. unsigned short polinomial = 0x0589;
  3. unsigned short crc = checksum_calculateCRC16(tmp, 2,polinomial);
  4. memcpy( tmp, &crc, 2 );
  5. return crc;
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. unsigned short checksum_calculateCRC16(unsigned char data[], unsigned short length, unsigned short polynomial)
  2. {
  3. unsigned char j, xor_flag, bit_mask, byte; // bit counter, XOR flag, bit mask, current byte
  4. unsigned short i; // byte counter
  5. unsigned short total_length = length + 2; // original length + two 0x00 bytes
  6. unsigned short remainder = 0xFFFF; // CRC remainder
  7.  
  8. xor_flag = 0x00;
  9.  
  10. // Process all bytes
  11. for(i = 0; i < total_length; i++)
  12. {
  13. // Set bit mask for next byte
  14. bit_mask = 0x80;
  15.  
  16. // Add two bytes with 0x00 after original data
  17. byte = 0x00;
  18. if(i < length)
  19. {
  20. byte = data[i];
  21. }
  22.  
  23. // Process all bits
  24. for(j = 0; j < 8; j++)
  25. {
  26. // If left-most bit is a 1
  27. if((remainder & 0x8000) == 0x8000)
  28. {
  29. // Set XOR flag
  30. xor_flag = 0x01;
  31. }
  32.  
  33. // Right-shift remainder
  34. remainder = remainder << 1;
  35.  
  36. // If current bit is a 1
  37. if((byte & bit_mask) == bit_mask)
  38. {
  39. // Insert a 1 at right-most position of remainder
  40. remainder++;
  41. }
  42.  
  43. // If XOR flag is set
  44. if(xor_flag == 0x01)
  45. {
  46. // XOR remainder with polynomial
  47. remainder ^= polynomial;
  48. // Clear XOR flag
  49. xor_flag = 0x00;
  50. }
  51.  
  52. // Shift mask to process next bit
  53. bit_mask = bit_mask >> 1;
  54. }
  55. }
  56.  
  57. // Return remainder
  58. return remainder;
  59. }
To copy to clipboard, switch view to plain text mode