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

Hey Monks,

I've been trying to check/create a .sfv file (A .sfv file just contain the filename and crc code next to it (in hex) in Perl here... And it should be easy, but the crc32 modules I use dont seem to generate correct crc for binary! (although they say they support files...)
Now, If I create a file called "text.txt" with "Just Anoter Perl Hacker" as content, the following code:
use String::CRC32; use Digest::Crc32; use strict; use warnings; use diagnostics; my $crc32 = new Digest::Crc32(); my $file = "text.txt"; open(SOMEFILE, $file); my $crc = crc32(*SOMEFILE); close(SOMEFILE); print "Dec: " . $crc . "\n"; printf("String::CRC32: %X\n", $crc); # upper case A-F $crc = $crc32->filecrc32($file); printf("Digest::Crc32: %X\n", $crc); # upper case A-F
prints:
Dec: 234160252
String::CRC32: DF5007C
Digest::Crc32: DF5007C

which is correct. If I do the same on a binary though, the crc doesnt match to some 3:rd party .sfv checker/creator (like hKSFV and QuickSFV)...

I would like some help to get this working in Perl!

Thanks,
Ace

Replies are listed 'Best First'.
Re: Doing a .sfv checker/creator in Perl...
by Corion (Patriarch) on Oct 05, 2005 at 08:06 UTC

    If the suggestion of Anonymous Monk above doesn't work for you, you should try the simpler and more available method of using binmode after opening your (binary) files, always:

    local *SOMEFILE; open(SOMEFILE, $file); binmode(SOMEFILE) my $crc = crc32(*SOMEFILE); close(SOMEFILE);
      Well, thank you. Seems like binmode(SOMEFILE); was all that was missing...
Re: Doing a .sfv checker/creator in Perl...
by Anonymous Monk on Oct 05, 2005 at 08:00 UTC
    Try Digest::CRC

    Try

    use open qw' IN :bytes ';
Re: Doing a .sfv checker/creator in Perl...
by GrandFather (Saint) on Oct 05, 2005 at 08:03 UTC

    How is your "binary" file different than your "text" file? Can you provide sample code that generates a binary file then CRCs it so we can reproduce your result? Showing the expected result for it would be good too.


    Perl is Huffman encoded by design.
      It was some .rar file... Hard to recreate here... :)

        Has it to be a .rar file to demonstrate the problem? The point is to create some trivial example that other monks can use to reproduce the problem and thus help solve it.

        It seems likely, as others have pointed out, that you have a line end tanslation problem and that controlling the binary/text mode of the file will fix the issue. But if it is something else you should try to find a way to reproduce the problem with a small piece of code. Just the process of finding a minimal example of the problem may will make the solution clear to you.


        Perl is Huffman encoded by design.
Re: Doing a .sfv checker/creator in Perl...
by blazar (Canon) on Oct 05, 2005 at 09:22 UTC
    I've been trying to check/create a .sfv file (A .sfv file just contain the filename and crc code next to it (in hex) in Perl here... And it should be easy, but the crc32 modules I use dont seem to generate correct crc for binary! (although they say they support files...)
    I can't be sure. But chances are you may need (to read about) binmode, or the :raw layer in open and PerlIO.