in reply to Re^4: CRC of a string
in thread CRC of a string

use strict; # solution: 990644297 my $buffer = "/etc/test2.sh"; my $crc = 0; for (grep $_, split /(.{1,4})/, $buffer) { my $val = sprintf "%02X"x4, map { ord } split //, $_; $crc += hex($val); $crc %= 2**32; } print $crc,$/;

Replies are listed 'Best First'.
Re^6: CRC of a string
by ikegami (Patriarch) on Nov 18, 2009 at 18:54 UTC

    /(.{1,4})/
    should be
    /(.{1,4})/s

    grep $_, split /(.{1,4})/s, $buffer
    is buggy. Fix:
    $buffer =~ /(.{1,4})/sg
    So much simpler too!

    hex sprintf "%02X"x4, map { ord } split //, $_
    can be simplified to
    unpack 'N', "$_\0\0\0"
    The latter should be much faster too.