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
some suggestions:
Minor Update: I thought a bit about truncating the checksum. The max value of 8 unsigned bits is 0xFF or#!/usr/bin/perl use strict; use warnings; my $BUFFSIZE = 4096 *1; sub Checksum { my ($FileName, $Start_byte, $Size) = @_; open (my $fh, '<', $FileName) or die "unable to open $FileName + for read $!"; binmode $fh; #This is truly bizarre! Checksum does not start at beginning o +f file! # seek ($fh, $Start_byte, 0) or die "Cannot seek to $Start_byte +on $FileName $!"; my $check_sum =0; # Allow for checkum only on a "window" of the input file, i.e. + $Size may be # much smaller than size_of_file - start_byte! Another Bizarr +e requirement!! while ($Size >0) { my $n_byte_request = ($BUFFSIZE > $Size) ? $Size : $BUFFSI +ZE; my $n_bytes_read = read($fh, my $buff, $n_byte_request); die "file system error binary read for $FileName" unless d +efined $n_bytes_read; die "premature EOF on $FileName checksum block size too bi +g for actual file" if ($n_bytes_read < $n_byte_request); my @bytes = unpack('C*', $buff); #input string of data ar +e 8 bit unsigned ints # check_sum is at least a 32 bit signed int. masking to 16 + bits # after every add probably not needed, but maybe. $check_sum += $_ for @bytes; $Size -= $n_bytes_read; } close $fh; $check_sum &= 0xFFFF; #Truncate to 16 bits, probably have to +do this more often... return $check_sum; } my $chk = Checksum('BinaryCheckSum.pl', 0,2); print $chk; #prints 68 decimal, 0x23 + 0x21, "#!"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Accessing individual bytes of a binary file as numerical values
by AnomalousMonk (Archbishop) on Apr 25, 2019 at 06:38 UTC | |
by Marshall (Canon) on Apr 25, 2019 at 07:13 UTC | |
by AnomalousMonk (Archbishop) on Apr 25, 2019 at 18:48 UTC | |
by Marshall (Canon) on Apr 28, 2019 at 20:10 UTC |