Hey everybody,
I'm having difficulty using the read command to perform a checksum operation. Whenever I print the final checksum, the result always returns FFFFFFFF, and I know thats not the checksum.
Code:
#!/usr/bin/perl -w
use strict;
use FileHandle;
use warnings;
my $data = "file.bin";
my $buffer;
my $string;
my $n = 0;
my $checksum = 0;
my $num = 0;
my $str = unpack("B32", pack("N", shift));
my $bits = 4;
my $decimal;
open FILE, $data or die $!;
binmode FILE;
while (($n = read FILE, $buffer, 4))
{
$decimal = unpack("N", pack("B32", $buffer));
{$checksum = $checksum + $decimal;
#$num = $num + $bits;}
#print "1. $num\n";
print "2. $decimal\n";
#print "3. $n bytes read\n";
print "4. $checksum \n";
}
$checksum = $checksum & 0xFFFFFFFF;
Basically, I'm trying to read a binary file 32 bits at a time by unpacking them into decimal and then adding them. Furthermore, I am trying to use the $checksum & 0xFFFFFFFFF command to restrain the checksum to 32 bits. Also, just ignore the commented out stuff, I was using it for my own testing. Any assistance would be greatly appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.