in reply to CRC of a string

See the  % template field prefix of unpack (search for '%' or 'prefix'):
>perl -wMstrict -le "my $str = '/etc/test2.sh'; $str .= qq{\0\0\0}; print unpack '%32N*', $str; " 990644297
(See pack for full documentation of template field specifiers, e.g., 'N', 'n', 'V', 'v', etc.)

Updates:

Replies are listed 'Best First'.
Re^2: CRC of a string
by keszler (Priest) on Nov 18, 2009 at 20:07 UTC
    Interesting! Your code works for me, with the same result, but when I try
    perldoc -f pack | grep %

    I get only 3 lines - the same three as found searching for '%' in pack - none of which mention the use of '%' in the (un)pack template.

      Right you are. My memory failed me. It is discussed in unpack and seems to be referred to as a 'template field prefix'. Search for '%' or 'prefix'. Reply fixed.

        Thank you very much for your great answers. So I could learn a lot.

        Here the final solution:

        #!/usr/bin/perl use strict; use warnings; my $str = "/etc/test2.sh"; # checksum32: 990644297 my $checksum32 = &calcChecksum32($str); print $checksum32 . "\n"; sub calcChecksum32() { my $str = $_[0]; my $nb_pad_bytes = 4 - (length($str) % 4); $str .= "\0" x $nb_pad_bytes; unpack('%32N*', $str); }