Hello everyone again,
I recently posted here with some help from ikegami, but I am still unable to produce the correct checksum (I am trying to match it to the checksum provided by my coworker). I would like to raed each 32 bit chunk of binary, and then turn it to decimal value, and add it to the checksum (which starts at 0). Does my ($n = read FILE, $buffer, 4) work? Is that reading 32 bits? Also, does my restraining work at the end of the script? Thanks for any and all help.
#!/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 %= 2**32;
}
Whenever i run this script, the number is never correct. Are there any obvious errors here?
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.