Read data structure from binary file

Read data structure from binary file

Post by hobz » Fri, 27 Mar 2009 19:17:36


I have a binary file which I would like to read into a data structure.
The data structure contains some simple data types like Integer16 and Integer32, but also a byte array of length 255.

How can I read this byte array into mathematica along with the other data types as a complete data structure, without having to use BinaryReadList(file, {"Integer32", "Byte", "Byte", "Byte", "Byte", "Byte", ..., "Byte", "Integer16"}) ?

Thanks.
 
 
 

Read data structure from binary file

Post by ragfiel » Sat, 28 Mar 2009 19:34:23


eger32, but also a byte array of length 255.
types as a complete data structure, without having to use BinaryReadList(f=
ile, {"Integer32", "Byte", "Byte", "Byte", "Byte", "Byte", ..., "Byte", "In=
teger16"}) ?

Join[{"Integer32"}, Table["Byte", {255}], {"Integer16"}]

-Rob

 
 
 

Read data structure from binary file

Post by dh » Sat, 28 Mar 2009 19:34:35


Hi,

if the file does not contain any information about the structure, it can

be interpreted in many ways. Therefore, you have to specify the

structure.

Note that you may assemble the list of structures definition

automatically, using e.b. Table, Append, Join...

Daniel
 
 
 

Read data structure from binary file

Post by Bill Row » Sat, 28 Mar 2009 19:37:47


I can think of two ways to approach this problem. One would be
to simply do BinaryReadList[file,"Byte"] and post process the
data. If there were a single 32-bit integer followed by a
sequence of bytes followed by a single 16 bit integer, then

Join[{FromDigits[#[[;;4]],256]}, #[[5;;-3]], {FromDigits[#[[-2;;]],256]}]&@=
BinaryReadList[file,"Byte"]

should do the trick.

The other approach would be to open a binary stream and do
something like

Flatten@{BinaryRead[stream, "Integer32"],BinaryReadList[stream,
"Byte", 255],BinaryRead[stream,"Integer32"]}

until you have read all of the data. Remember to close the
stream after you are done.