4

I have read about a helpful website with a CRC calculator covered in other posts, which works great, but the code provided in C language does not replicate the capabilities, unfortunately: http://www.zorc.breitbandkatze.de/crc.html

The following configuration is required for CRC16-CCITT (x.25), which is the format I am working with for a sensor and supporting electronic assembly:

  • CRC Order: 16
  • CRC polynomial: 0x11021 [entered as 1021]
  • Initial value: 0xffff [entered as ffff]
  • Direct/Indirect: Direct
  • Final XOR Value: 0xffff [entered as ffff]
  • Reverse Data Bytes: True
  • Reverse CRC Result Before Final XOR: True

However, sometimes an incorrect CRC is calculated using the C code, specifically, but comes out correct with the online calculator.

You can find the C code at the following link, but make sure to change the configuration options to match the above: http://www.zorc.breitbandkatze.de/crctester.c

Here is a sample packet placed in the C Code:

const char string[] = { "\x82\x2f\x0a\x40\x00\x00\x7a\x44" };

This returns an incorrect CRC of "0x1e4e".

and in the online calculator: "%82%2f%0a%40%00%00%7a%44"

Which returns the correct CRC of "0xd831".

Also, you will want a packet that works with BOTH the online calculator AND the C Code, which follows.

In the C Code:

const char string[] = { "\x81\x2f\x0b\x4f\xd8\xab\x0d\x42\xed" };

And in the online calculator: "%81%2f%0b%4f%d8%ab%0d%42%ed"

Does anyone know why this is happening, and how the C Code can be corrected to fix this?

Thank you so much for considering to help!

KappaDragon
  • 61
  • 1
  • 6
  • 10
    Trivial error in the C source code. The length of the string is computed with `strlen()`, but your "string" embeds a `\x00` NUL byte, so strlen computes a shorter than actual length (it assumes the string terminates at the first NUL), and so you get a different CRC. When you replace occurences of `strlen(string)` by `sizeof(string)-1`, it works. – Iwillnotexist Idonotexist Nov 08 '15 at 06:35
  • 1
    @IwillnotexistIdonotexist: That is an answer. Actually, that is the answer. Why didn't you make it an answer instead of a comment? – Mark Adler Nov 08 '15 at 10:18
  • I just tested the suggestion and it works completely. I should note that the code has some outdated items in it, and required some functional updates, but overall it works perfectly now. If anyone would like a copy of the updated/functional code, please do not hesitate to contact me. – KappaDragon Nov 09 '15 at 03:52

0 Answers0