7

I am analyzing hex data from binary data dumps from a basic command-line program of mine. I'm basically dumping the exact contents of a struct (a large array of structs, actually) to a text file.

I then create a second binary dump, and compare the two files in vim using xxd to create binary-to-text representations of the original data.

Both files are the exact same size in bytes, and I'm trying to compare the two in a meaningful way. Even a small change in the data before I dump the file results in a large change in other parts of the file, due to other sections containing hashes, functions based on the value I changed, and so forth.

Is it possible to tell diff or vimdiff to say, compare two files, and show me only the parts of the file where in the original file (ie: file 1) a value was set to 1, and in the second file, the value was set to 32?

Thank you!

idmean
  • 14,540
  • 9
  • 54
  • 83
Cloud
  • 18,753
  • 15
  • 79
  • 153

1 Answers1

18

I use:

diff <(xxd file1.bin) <(xxd file2.bin)

This uses process substitution to compare the output of two xxd processes. Note that this still shows line differences, so if any byte on a line is different it will be listed. This gives a nice hexdump-looking comparison.

The classical tool for this however, is cmp.

So, this could be handled like so:

cmp -l file1.raw file2.raw | grep -in "oldValue" | grep -in "newValue"

This will list exactly what you need, with the following fields printed out:

OFFSET  VALUE_IN_FILE_1 VALUE_IN_FILE_2
Cloud
  • 18,753
  • 15
  • 79
  • 153
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • That offers a text-mode comparison of the output of two binary-to-text files, which I'm already generating in `vim`. How does this address the problem of field-specific comparison given that I know that a single field will start at value `x` and in the second file will be value `y`? – Cloud May 05 '13 at 01:28
  • 2
    I don't understand the problem. `cmp -l` will "Print the byte number (decimal) and the differing byte values (octal) for each difference." Just `grep` through that list for the known values, and it will give you the decimal offset. – Jonathon Reinhart May 08 '13 at 21:08
  • 1
    Now I understand what you meant. Thanks for your patience! – Cloud May 09 '13 at 17:13
  • 1
    a slight variation may be: vim -d <(xxd fileA) <(xxd fileB) – J Jorgenson Jan 13 '14 at 16:45