http://www.perlmonks.org?node_id=974857

jedikaiti has asked for the wisdom of the Perl Monks concerning the following question:

I am having a bit of a problem with Digest::CRC. I am testing some software handling bad packet headers, and I need to generate CRC8 checksums for a number of packets. However, when I use Digest::CRC to get the checksum for my sanity check packet, I'm not getting what I expect.

According to the best information I have, I should be getting 234 (or EA hex) for the string 7AEE840000187A011B80C001001100119103F3267D000000000000FF00000000. Instead I am getting 236 (or EC hex). Before I go chasing after errors elsewhere, I thought it might be best to run a sanity check past you guys, in case I am doing something wrong in my Perl and not seeing it. I appreciate your help. The code below is what is returning 236/0xEC when I expect 234/0xEA.

#!/usr/bin/perl -w use strict; use warnings; use Math::BaseCnv; use Digest::CRC qw(crcccitt crc crc8 crcopenpgparmor); my $pkt = "7AEE840000187A011B80C001001100119103F3267D000000000000FF000 +00000"; my $crc = crc8($pkt); print "$crc\t". cnv($crc,10,16) ."\n";
Kaiti
Swiss Army Nerd

Replies are listed 'Best First'.
Re: Calculating CRC8 using Digest::CRC
by Athanasius (Archbishop) on Jun 07, 2012 at 02:48 UTC
    According to the best information I have, I should be getting 234 (or EA hex) for the string 7AEE840000187A011B80C001001100119103F3267D000000000000FF00000000. Instead I am getting 236 (or EC hex).

    Converting 7AEE840000187A011B80C001001100119103F3267D000000000000FF00000000 to hex in the online calculator here gives: 37414545383430303030313837413031314238304330303130303131303031313931303346333236374430303030303030303030303046463030303030303030.

    Calculating the CRC-8 for this hex string in the online calculator here gives 0xec, as per your results with Digest::CRC.

    So, why do you expect a result of 0xea?

    HTH,

    Athanasius <°(((><contra mundum

      It was given to me by someone else whose source REALLY should be accurate. Since 2 other Monks are telling me otherwise, however, I'll be grilling him about his confidence in his source tomorrow.

      Thanks!

      Kaiti
      Swiss Army Nerd

        Just to be really, really clear about the point that Athanasius is making...
        The two character string '7A' consists (in ASCII) in the hex bytes 0x37 and 0x41 (the characters '7' and 'A' respectively). OTOH, it is possible to have a single character ASCII string that consists in the single hex byte 0x7A (the character 'z').
        In the example you give in your OP, which is which?

Re: Calculating CRC8 using Digest::CRC
by Anonymous Monk on Jun 07, 2012 at 02:55 UTC

    Where do you get 7AEE840000187A011B80C001001100119103F3267D000000000000FF00000000 from, how do you know what its supposed to CRC8 as?

    If you go to Cyclic redundancy check you'll see a bunch

    CRC-8-CCITT CRC-8-Dallas/Maxim CRC-8 CRC-8-SAE J1850 CRC-8-WCDMA

    Digest::CRC mentions CRC-CCITT which I assume is CRC-8-CCITT

    This is as far as I looked :) because I couldn't find sample values -- I despise bit shifting and pointless word problems