MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Read data structure from binary file

  • To: mathgroup at
  • Subject: [mg98001] Re: Read data structure from binary file
  • From: Bill Rowe <readnews at>
  • Date: Fri, 27 Mar 2009 05:38:04 -0500 (EST)

On 3/26/09 at 5:18 AM, hobz at (hobz) wrote:

>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"}) ?

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]}]&@=

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.

  • Prev by Date: Looking for help with "Reduce" command for use with my Calculus course.
  • Next by Date: Re: How do I hide all input in a Mathematica notebook?
  • Previous by thread: Re: Read data structure from binary file
  • Next by thread: Numerical Optimization with Parameters CI needed