Search code examples
perlhexdump

Native perl hexdump -- just checking for zero values


I'm trying to check to see if a disk or disk image is 'empty'. I'm going to assume that this is true if the first 1mb and last 1mb are zeroes. I started by trying to recreate hexdump but it seems a little convuluded at this point.

Here's roughly my code:

open DISK, $disk or die $!;
for( 1 .. 1024 ) {
    $buffer = undef;
    sysread(DISK, $buffer, 1024, 0) or last;
    for ( split //, $buffer ) {
        if( ord($_) =~ /[^0]/ ) {
            $flag++;
        }
    }
}

Is there a better way to do this?


Solution

  • Check directly if the byte string $buffer contains anything other than \0 bytes.

    if ($buffer =~ /[^\0]/) {
        $flag++;
    }