your read statement should be correct, since you've used binmode on the filehandle and don't have a :utf8 pragma. you're reading 4 x 8bit bytes.

The final read is not guaranteed to be 32 bits, you should check $n to see how many bytes were read from the stream. I'm not sure of the behavior of unpack if you don't feed it enough data. Since you're using Big-Endian, it may see your final read as much larger than expected.

Your final line $checksum %= 2^32, is going to constrain your checksum to be mod 2^32. If that's what you want, then, yes, it should work.

You're doing your bit match in Big-Endian. Is there any chance that the code you're trying to match does it little-endian? Your B in unpack and N in pack match endianness, so that's good.

Suggestion: make a 4 byte test file and run the other checksum program on it. Use this as the basis for your first test of your checksum program. Then progress to a 5 yte test file, to test the question about the truncated final read.

perldoc read:

Note the characters: depending on the status of the filehandle, either (8-bit) bytes or characters are read. By default all filehandles operate on bytes, but for example if the filehandle has been opened with the :utf8 I/O layer (see "open", and the open pragma, open), the I/O will operate on UTF-8 encoded Unicode characters, not bytes.

In reply to Re: Difficulty producing correct checksum by spazm
in thread Difficulty producing correct checksum by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.