in reply to Re: Accessing individual bytes of a binary file as numerical values
in thread Accessing individual bytes of a binary file as numerical values
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: <%-{-{-{-<
|
|---|