in reply to CRC of a string

String::CRC

Or if that CRC check doesn't meet your needs: Math::BigInt

Replies are listed 'Best First'.
Re^2: CRC of a string
by Dirk80 (Pilgrim) on Nov 18, 2009 at 14:57 UTC

    Thank you for your answer. But I get another result as with my code.

    use String::CRC32; my $crc32 = crc32("/etc/test2.sh"); print $crc32 . "\n";

    Here I get the result 1561480701, but the result of my previous code was 990644297. What could be the reason for this?

      String::CRC32 is not performing the same calculation as you. If you just need a CRC, I thought it might do.

      Since you appear to need the specific CRC of your calculations, Math::BigInt allows you to avoid the integer overflow.

        Ok, I did not know that there exist different versions of how to compute a CRC32. But unfortunately I need this specific one.

        Thank you very much for the hint with the Math::BigInt. Now I could get rid of the warnings.

        Here the new code:

        #!/usr/bin/perl use strict; use warnings; use Math::BigInt; # solution: 990644297 my $buffer = "/etc/test2.sh"; my $crc = Math::BigInt->new('0'); &determineCrc32($buffer, $crc); print $crc . "\n"; sub determineCrc32() { my $buffer = $_[0]; my $crc = $_[1]; my $i = 0; my $val = 0; my $k = 0; for( $i = 0; $i < int(length($buffer) / 4); $i = $i + 1 ) { $val = sprintf("%02X", ord(substr($buffer, ($i*4) + 0, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 1, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 2, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 3, 1))); $crc->badd(hex($val)); $crc->bmod('0x100000000'); } $val = ""; $k = 0; for( 1 .. int(length($buffer) % 4) ) { $val .= sprintf("%02X", ord(substr($buffer, ($i*4) + $k, 1))); $k++; } # pad other bytes of 32bit with "00" for( 1 .. (4 - int(length($buffer) % 4)) ) { $val .= "00"; } $crc->badd(hex($val)); $crc->bmod('0x100000000'); }

        Would be very interesting for me how a good perl programmer would code this specific CRC. Because I don't think that my solution is a good way to solve it.

        Greetings

        Dirk