I'm trying to work with .bin file of specific format. I know exactly the type and place of each element in this bin. Is there any elegant way of reading such files?
In Python I can do it using struct module. But I use R for my application and it's highly undesirable to use another programming language.
My Python code:
import struct
struct_fmt = '=HHHHHHH80h80hhhhffff20fhh4fHHH5h' # int[5], float, byte[255]
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from
results = []
with open("14.09.20-23.52.55.292.bin", "rb") as f:
while True:
data = f.read(struct_len)
if not data: break
s = struct_unpack(data)
results.append(s)
I came across some doubtful ways, but I wonder if there is a more elegant variant.