Python Forum

Full Version: Comparing two files data while one got hex and other binary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python. I am trying trying to use it for the problem as follows:
I have one file with 3 coloums of 32 bit data in hex representation. Second file is also 3 coloums but showing only one or two bits in binary representation that is my look up table.
Comparing two files records in such a way that on each iteration from first file picking one combination of bits (2bits) for 3 fields in record compare them to record in look up table I.e file 2.
E.g
File 1 record hex:
A8000001 0000000 64000000

File 2 data binary :
10 00 01
One iteration picks 01 compares to file2 first coloums data I.e 10 then file 1,2nd coloums 2 bits 00 with 2nd file 2nd coloums in same row it's 00. So on.
Kindly suggest a solution or a hint how to implement it.
Hex and binary are just representations of data, unless you have two text files, one with hex representation, and one with binary.
Let me explain:
The following is the same number:
decimal: 1347852
binary: 101001001000100001100
hex: 14910C
octal: 5110414
base26: 2OHMC

It's just a representation
If the files are not big, you can load both in memory.
with open('data.hex') as hex_fd:
   hex_content = hex_fd.read()
Same with the binary file, but in mode 'rb' (read binary).

Then you need binascii to change the representation from ascii_hex to binary.
import binascii
hex_value = 'FF'
bytes_value = binascii.a2b_hex(hex_value)
# binascii.unhexlify is the same function
Then you compare the bytes_value with the bytes from the binary file.
If they are too big for your memory, you can approach an iterative way.
Thanks very much for response. Just to clarify here. They both are text files, one got binary data and other got hex data.with each data entry separated by space/ comma.
File with binary is for one pin of microcontroller which would show all the right expected combination of bits results.so will be used as blue print for all pins. The only issue is 32bit file got hex data from which 2 bits need to be picked based on number of pin to check in iteration.