I need to compare two binary files and get output in form

for every different byte. So if file1.bin is

  00 90 00 11

in binary form

and file2.bin is

  00 91 00 10

I want to get something like

  00000001 90 91
  00000003 11 10

what is the easiest way to accomplish the goal? Standard tool? Some 3rd party tool?

Note: cmp -l should be killed with fire, it uses a decimal system for offsets and octal for bytes.

link|improve this question
2  
you're basically looking for "binary diff". i can imagine some reeeally ugly commandline one-liner with od... – quack quixote Mar 29 '10 at 15:36
@quack quixote: What's ugly about a one-liner? ;) – Bobby Mar 29 '10 at 16:50
feedback

5 Answers

up vote 3 down vote accepted

This will print the offset and bytes in hex:

cmp -l file1.bin file2.bin | awk '{printf "%08X %02X %02X\n", $1, strtonum(0$2), strtonum(0$3)}'
link|improve this answer
feedback

as ~quack (hehe) pointed out:

 % xxd b1 > b1.hex
 % xxd b2 > b2.hex

and then

 % diff b1.hex b2.hex

or

 % vimdiff b1.hex b2.hex 
link|improve this answer
2  
In Bash: diff <(xxd b1) <(xxd b2) but the output format of this (or yours) is nowhere near what the OP asked for. – Dennis Williamson Mar 29 '10 at 16:33
with vimdiff it is, it will color the bytes in the lines where the two 'files' differ – akira Mar 30 '10 at 4:45
Aww, why didn't I think of that? And I'm sure I've used this technique in the past too. – njd Mar 30 '10 at 17:37
feedback

There's a tool call DHEX which may do the job.

And there's another tool called VBinDiff

For a strictly command-line approach, try JDIFF

link|improve this answer
DHEX is awesome is comparing binaries is what you want to do. Feed it two files and it takes you right to a comparative view, highlighting to differences, with easy ability to move to the next difference. Also it's able to work with large terminals, which is very useful on widescreen monitors. – Marcin Sep 8 '11 at 0:08
feedback

May not strictly answer the question, but I use this for diffing binaries:

gvim -d <(xxd -c 1 ~/file1.bin | awk '{print $2, $3}') <(xxd -c 1 ~/file2.bin | awk '{print $2, $3}')

It prints both files out as hex and ascii values, one byte per line, and then uses vim's diff facility to render them visually.

link|improve this answer
feedback

There is another one that I like, it's named Beyond Compare. You can find it here:

http://www.scootersoftware.com/moreinfo.php?zz=moreinfo_compare

link|improve this answer
feedback

Your Answer

 
or
required, but never shown