Just a one-shot, throwaway script I quickly concocted up yesterday, which I... don't feel like throwing away altogether: perhaps it will be instructive for others, although I warn in advance that for generic use it would require quite some massaging.

The problem: two big files (of the same actual size) which would definitely seem to be the same file, but indeed are not, as a checksum on each shows. I want to show the actual differences:

#!/usr/bin/perl use strict; use warnings; $|++; die "Usage: $0 file1 file2\n" unless @ARGV==2; my ($f1, $f2) = map { open my $fh, '<:raw', $_ or die "Can't open `$_': $!\n"; $fh } @ARGV; $/ = \0x100_000; while (my $s1=<$f1>) { defined +(my $s2=<$f2>) or last; printf "Block %04d: no differences\r", $. and next if $s1 eq $s2; my @l = map length, ($s1 ^ $s2) =~ /^(\0*)(.+?)(\0*)\z/; printf "\nBlock %04d: A=[0 x %d], B=[* x %d], C=[0 x %d]\n", $., @ +l; print "\tB=[@{[ unpack '(H2)*' => substr $_, $l[0], $l[1] ]}]\n" for $s1, $s2; } __END__

Funny detail, not only did the two files ended up differing by one single byte, but more precisely, by one single bit:

C:\temp>perl compare.pl foo bar Block 0727: no differences Block 0728: A=[0 x 759818], B=[* x 1], C=[0 x 288757] B=[e7] B=[ef] Block 0973: no differences
--
If you can't understand the incipit, then please check the IPB Campaign.

Replies are listed 'Best First'.
Re: Finding differences in binary files
by Tux (Canon) on Sep 01, 2008 at 23:25 UTC

    Nothing wrong with using perl, but I'd just use cmp


    Enjoy, Have FUN! H.Merijn
      fc /b since he's using Windows
      ... or diff or gvimdiff, for that matter. Clearly, these are only available on Unix ... unless you have Perl Power Tools installed on your Windows Machine.

      Cheers -

      Pat

        Or on windows ...

        And for me a Windows system is close to unuseable without Cygwin, which has all the goodies in by default, but ikegami already mentioned fc, which seems to fit the bill for Windows.


        Enjoy, Have FUN! H.Merijn