in reply to CRC-16 algorithm

I found string::CRC on CPAN.

Replies are listed 'Best First'.
(String::CRC) Re: CRC-16 algorithm
by CMonster (Scribe) on May 20, 2003 at 20:14 UTC

    Thanks! OK, got it and tried it. The documentation is a bit sparse, but using String::CRC gives rise to a program like so:

    use strict; use warnings; use String::CRC; my $message = $ARGV[0]; my $crc16 = crc($message); print "CRC16 in decimal: $crc16\n"; print "CRC16 in hex:\n ", unpack('H*', pack('S', $crc16)), "\n";

    Running the program gives this output:

    cradcliff% ./crc16-modular.pl foo CRC16 in decimal: 1736882148 CRC16 in hex: b7e4 cradcliff% ./crc16-modular.pl foobar CRC16 in decimal: 3375757715 CRC16 in hex: f993 cradcliff% ./crc16-modular.pl foobaz CRC16 in decimal: 3375757723 CRC16 in hex: f99b

    More simply:

    'foo'    => 0xb7e4
    'foobar' => 0xf993
    'foobaz' => 0xf99b
    

    Can anyone verify that these are correct values for a CRC-16 checksum, assuming the conditions I specified earlier? Alternately, can anyone supply sample string-checksum pairs I can use to test this program?

    Thanks, ~c

      Try crc($message,16); because the default is 32 (like the pod says).


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Yes, I forgot to add that. Unfortunately, even with the change, the result is:

        cradcliff% ./crc16-modular.pl 123456789
        CRC16 in decimal: 4535
        CRC16 in hex:
          11b7
        

        ...which doesn't match the 0xBB3D result provided by the on-line calculator. I'm guessing that String::CRC doesn't use the CRC-16 polynomial or some other CRC-16-specific setting.

        I'll check my other algorithm against the on-line calculator. If anyone has algorithmic hints, please post them.