Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8645

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

From "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Reading Single and Double reals in Little-endian and Big-endian
Date 2012-02-20 07:54 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jhsu7g$np4$1@smc.vnet.net> (permalink)
References <jhldir$ot7$1@smc.vnet.net>

Show all headers | View raw


On Fri, 17 Feb 2012 11:27:23 -0000, ezh <dgolber@aol.com> 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.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Reading Single and Double reals in Little-endian and Big-endian ezh <dgolber@aol.com> - 2012-02-17 11:27 +0000
  Re: Reading Single and Double reals in Little-endian and Big-endian "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-20 07:54 +0000
    Re: Reading Single and Double reals in Little-endian and Big-endian ezh <dgolber@aol.com> - 2012-02-24 06:02 +0000
      Re: Reading Single and Double reals in Little-endian and Big-endian David Bailey <dave@removedbailey.co.uk> - 2012-02-25 06:55 +0000
    Re: Reading Single and Double reals in Little-endian and Big-endian "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-26 09:21 +0000

csiph-web