Python Forum
Comparing two files data while one got hex and other binary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing two files data while one got hex and other binary
#1
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.
Reply
#2
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
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,072 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  script to calculate data in csv-files ledgreve 0 1,112 May-19-2023, 07:24 AM
Last Post: ledgreve
  SQL Alchemy help to extract sql data into csv files mg24 1 1,794 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  Apply textual data cleaning to several CSV files ErcoleL99 0 846 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  Including data files in a package ChrisOfBristol 4 2,559 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  How to convert binary data into text? ZYSIA 3 2,655 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  Plotting sum of data files using simple code Laplace12 3 3,062 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  How do use data from csv files as variables? JUSS1K 1 2,160 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  Binary data to Image convert Nuwan16 1 5,697 Aug-24-2020, 06:03 AM
Last Post: millpond
  Comparing Items Different Data Frames With a WHILE Loop JoeDainton123 1 1,955 Jul-30-2020, 04:11 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020