MathGroup Archive 2012

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

Search the Archive

Re: Reading Single and Double reals in Little-endian and Big-endian

  • To: mathgroup at
  • Subject: [mg125070] Re: Reading Single and Double reals in Little-endian and Big-endian
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at>
  • Date: Mon, 20 Feb 2012 02:50:33 -0500 (EST)
  • Delivered-to:
  • References: <jhldir$ot7$>

On Fri, 17 Feb 2012 11:27:23 -0000, ezh <dgolber at> wrote:

> I am running Mathematica 4 on a PC.
> I am reading a file (an STL file), which has 32-bit integers and 32-
> bit reals in little-endian format.  I can read in the integers
> correctly, using
> numTriangles = ReadBinary[strm, Int32, ByteOrder ->
> LeastSignificantByteFirst];
> However, the documentation states (and is correct) that ByteOrder ->
> LeastSignificantByteFirst only affects reading integers.  So far, I
> have not figured out how to read 32-bit reals ("Single") correctly.
> I could read in the 32-bits as bytes, and reverse the order, and build
> a 32-bit integer with the correct bits in it to be the desired 32-bit
> real.  But how do I get Mathematica to regard those 32-bits as an IEEE
> real number?
> I'd hate to have to read up on the IEEE format and build the value
> myself.
> I can't be the first person in theworld to hit this problem!
> And why (futile question) doesn't mathematica know about big- and
> little-endian for _reals_ as well as for _integers_?
> Thanks
> Dave Golber

It does. However, this support (at least as concerns BinaryRead) was added  
in Mathematica 5.2 (released in June 2005). So, no, you're not the first  
person in the world to encounter this problem, but you might be the last  
person in the world still to be using Mathematica 4.

Nonetheless, Mathematica 4's Import is documented as accepting the  
ByteOrdering option (taking values of +1 or -1 representing big- and  
little-endian formats respectively); whether this applies to  
floating-point as well as integer values I do not know off-hand (as it has  
been a very long time since I used Mathematica 4) but the implementation  
is certainly different to that in the Utilities`BinaryFiles` package you  
seem to be using, and in more recent versions of Mathematica the support  
is there, so I would encourage you to give it a try.

However, I wonder why you need to specify the byte ordering at all, as STL  
files use little-endian ordering, and so do PCs, so no conversion ought to  
be necessary. Or, indeed, why you have to import the file using  
Utilities`BinaryFiles`ReadBinary to read values from a stream rather than  
Import["file.stl"], which should do the job with much less effort. In  
fact, it seems to me as if you may be using code written for Mathematica 3  
on a Mac (where these issues certainly would need to be dealt with), which  
is in need of updating for Mathematica 4.

  • Prev by Date: Re: Iterative solution to a transcendental equation.
  • Next by Date: Re: How best to implement a hash table in Mathematica
  • Previous by thread: Reading Single and Double reals in Little-endian and Big-endian
  • Next by thread: Re: Reading Single and Double reals in Little-endian and Big-endian