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 ?

hakunamatata
  • 127
  • 1
  • 14

2 Answers2

5

No.

>>> import struct
>>> struct.unpack('<f', '\xc0\x05\xe5\x3f')
(1.7892379760742188,)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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]
travelingbones
  • 7,919
  • 6
  • 36
  • 43