MathGroup Archive 2002

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

Search the Archive

FromBytes[]?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32868] FromBytes[]?
  • From: franki at aerodyne.com (Frank J. Iannarilli, Jr.)
  • Date: Sat, 16 Feb 2002 04:35:26 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

I want to convert a group/stream of Bytes to various internal
Mathematica numerical types, particularly into Real (machine precision
number).  The motivation is the fast reading of binary files.

I don't want to deal with the Std Package
Utilities`BinaryFiles`ReadBinary or Developer'BinaryImport, which do
have the conversion utilities (e.g. into Real32, etc), since they are
SLOW or otherwise clunky.

When employing the suggested fast reading of binary files: 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
There are numerous means of reading data into Mathematica from binary
files.  However, most of them are too slow.  Through experience and
some newsgroup research, I've found the fast easy way:

stream = OpenRead["Cals9", DOSTextFormat -> False];
   (* Note:  this DOSTextFormat->False option is key to using
ReadList[] in binary mode; otherwise, it wants to digest text.  Readin
Byte chunks, then post-format.  Remarkably, the DosTextFormat option
is not mentioned/discussed anywhere in the Mathematica book or Help
Browser *)

SetStreamPosition[stream, 78*131072];
     (* Position to desired read starting location within file...count
is in Bytes, and is index base = 0  *).

data = ReadList[stream, Byte, 131072];
      (* Reads 131072 Bytes into linear List "data" *)

Close[stream];
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


you end up with a list of Bytes.  Converting it to 16-bit integers is
easy:

data = Map[(#1[[1]] + 256*#1[[2]] & ), Partition[data, 2]];
  (* takes original Byte List "data" and makes 16-bit "words", writing
over List "data".  Result is half-as-long List of words.  *)


...but how would one convert it to real numbers (given that every 4
bytes comprises an IEEE single-precision number)?

Thanks.

franki at aerodyne.com


  • Prev by Date: RE: Iterators
  • Next by Date: Re: Very urgent problem...PLEASE help me!
  • Previous by thread: Re: Tracing Evaluation
  • Next by thread: CAlculus Wiz