while ( $ChecksumLoopCounter < $Size ) { unless ( sysread ( $FileHandle, $BufferByte, 1) == 1 ) { die "sysread size error" ; } my $Byte = ord($BufferByte); $Checksum = unpack("S" , pack("S", $Checksum + $Byte)); $ChecksumLoopCounter++ ; $ChecksumByteOffset++ ; } close $FileHandle ; return $Checksum;
It goes without saying that I don't understand the requirements and constraints of your task. For some reason, you are constrained to read the file byte-by-byte with sysread et al. I must agree that trying to use pack and unpack in this situation is awkward. I would be inclined toward something like (untested):
See integer.use integer; my $Checksum = 0; while ($Size--) { unless ( sysread ( $FileHandle, $BufferByte, 1) == 1 ) { die "sysread size error" ; } $Checksum += ord($BufferByte); } close $FileHandle ; return $Checksum & 0xffff;
Give a man a fish: <%-{-{-{-<
In reply to Re^2: Accessing individual bytes of a binary file as numerical values
by AnomalousMonk
in thread Accessing individual bytes of a binary file as numerical values
by Chris01234
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |