cts

Majority wins hex diff

cts
Jul 2nd, 2024
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. # Function to convert file to hex
  4. def file_to_hex(filename):
  5.     with open(filename, 'rb') as f:
  6.         return f.read().hex()
  7.  
  8. # List of your files
  9. files = ["testfile1", "testfile2", "testfile3", "testfile4"]  
  10.  
  11. # Convert files to hex
  12. hex_files = [file_to_hex(file) for file in files]
  13.  
  14. # Align hex values
  15. max_len = max(len(h) for h in hex_files)
  16. aligned_hex_files = [h.ljust(max_len, '0') for h in hex_files]
  17.  
  18. # Function to compare hex files and determine differences
  19. def compare_hex_files(hex_files):
  20.     differences = []
  21.     num_files = len(hex_files)
  22.     length = len(hex_files[0])
  23.    
  24.     for i in range(0, length, 2):
  25.         chunk = [hex_file[i:i+2] for hex_file in hex_files]
  26.         unique_chunks = set(chunk)
  27.        
  28.         if len(unique_chunks) == 1:
  29.             differences.append('ALL_SAME')
  30.         elif len(unique_chunks) == num_files:
  31.             differences.append('ALL_DIFFERENT')
  32.         else:
  33.             differences.append('MAJORITY_WINS')
  34.    
  35.     return differences
  36.  
  37. # Compute differences
  38. diffs = compare_hex_files(aligned_hex_files)
  39.  
  40. # Simple text-based output of differences
  41. for i, diff in enumerate(diffs):
  42.     print(f'Byte {i:04x}: {diff}')
  43.  
  44. # Advanced visualization using matplotlib
  45. colors = {'ALL_SAME': 'green', 'ALL_DIFFERENT': 'red', 'MAJORITY_WINS': 'yellow'}
  46. color_list = [colors[diff] for diff in diffs]
  47.  
  48. plt.figure(figsize=(12, 6))
  49. plt.bar(range(len(diffs)), [1] * len(diffs), color=color_list)
  50. plt.xlabel('Byte Position')
  51. plt.ylabel('Difference')
  52. plt.title('Hex File Comparison')
  53. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment