1

I want to read a binary data file that contains 32 bit floating point binary data in python. I tried using hexdump on the file and then reading the hexdump in python. Some of the values when converted back to float returned nan. I checked if I made a mistake in combining the hexdump values but couldn't find any. This is what I did this in shell:

hexdump -vc >> output.txt

The output was of the form c0 05 e5 3f ... and so on

I joined the hex as : '3fe505c0'

Is this the correct way to do this ?

2 Answers 2

5

No.

>>> import struct
>>> struct.unpack('<f', '\xc0\x05\xe5\x3f')
(1.7892379760742188,)
Sign up to request clarification or add additional context in comments.

7 Comments

Okay Sir , so if I do struct.unpack('<f', hex.decode('hex'))[0] where hex is 'c005e53f' is that correct ?
Read the bytes directly from the binary file and use those.
Okay Sir. So like hex = file.read(8) and feed that into the unpack command ?
32 bits is 4 bytes, but yes.
I tried with open ('data', 'r') as file , byte = file.read(4) , print struct.unpack('<4', byte.decode('hex'))[0]. It throws the error : Non hexadecimal digit found .
|
0

Here's an example for writing, the reading the file (so you can see you get the right answer modulo some rounding errors).

import csv, os
import struct

test_floats = [1.2, 0.377, 4.001, 5, -3.4]

## write test floats to a new csv file:
path_test_csv = os.path.abspath('data-test/test.csv')
print path_test_csv
test_csv = open(path_test_csv, 'w')
wr = csv.writer(test_csv)
for x in test_floats:
    wr.writerow([x])
test_csv.close()


## write test floats as binary
path_test_binary = os.path.abspath('data-test/test.binary')
test_binary = open(path_test_binary, 'w')
for x in test_floats:
    binary_data = struct.pack('<f', x)
    test_binary.write(binary_data)
test_binary.close()


## read in test binary
binary = open(path_test_binary, 'rb')
binary.seek(0,2) ## seeks to the end of the file (needed for getting number of bytes)
num_bytes = binary.tell() ## how many bytes are in this file is stored as num_bytes
# print num_bytes
binary.seek(0) ## seeks back to beginning of file
i = 0 ## index of bytes we are on
while i < num_bytes:
    binary_data = binary.read(4) ## reads in 4 bytes = 8 hex characters = 32-bits
    i += 4 ## we seeked ahead 4 bytes by reading them, so now increment index i
    unpacked = struct.unpack("<f", binary_data) ## <f denotes little endian float encoding
    print tuple(unpacked)[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.