Skip to content

Instantly share code, notes, and snippets.

@phnahes
Created April 28, 2025 18:50
Show Gist options
  • Save phnahes/5c90d1349c030dee9647126774f264f4 to your computer and use it in GitHub Desktop.
Save phnahes/5c90d1349c030dee9647126774f264f4 to your computer and use it in GitHub Desktop.
Compare two binary files and show the differences (byte offset and values).
#!/usr/bin/env python3
"""
compare_bins.py - Compare two binary files and print the differences.
Usage:
python compare_bins.py original.bin modified.bin
Example output:
Offset 0x1234: 56 → 78
"""
import sys
def compare_bins(file1, file2):
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
bin1 = f1.read()
bin2 = f2.read()
size = min(len(bin1), len(bin2))
diffs = []
for i in range(size):
if bin1[i] != bin2[i]:
diffs.append((i, bin1[i], bin2[i]))
if len(bin1) != len(bin2):
print(f"Warning: The files have different sizes! {len(bin1)} vs {len(bin2)}")
return diffs
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python compare_bins.py file1.bin file2.bin")
sys.exit(1)
file1 = sys.argv[1]
file2 = sys.argv[2]
differences = compare_bins(file1, file2)
if not differences:
print("No differences found!")
else:
for offset, byte1, byte2 in differences:
print(f"Offset 0x{offset:04X}: {byte1:02X} → {byte2:02X}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment