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


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

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-26 09:21 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jicti8$em3$1@smc.vnet.net> (permalink)
References <jhldir$ot7$1@smc.vnet.net> <jhsu7g$np4$1@smc.vnet.net>

Show all headers | View raw


On Fri, 24 Feb 2012 06:02:04 -0000, ezh <dgolber@aol.com> wrote:

> Yes, I need to reverse the byte order.  After all, it's not a PC
> storing the stuff read from the file, it's Mathematica.

I see. Well, your problem is caused by the Utilities`BinaryFiles` package,  
which reads binary files in a fairly insane way and hard-codes an  
assumption of big-endianness when dealing with reals. This code was  
written over 20 years ago (in 1991) and is now well and truly obsolete. On  
the plus side, the package source (in  
AddOns/StandardPackages/Utilities/BinaryFiles.m) can be edited as you see  
fit, including to add support for little-endian reals. So, for example, in  
the package we find the following definition which provides for reading a  
single precision number:

rb[stream_InputStream,Single,_] :=
   floatvalue @@ {Quotient[#1,128],
         (Mod[#1,128])*2 + Quotient[#2,128],
         ((Mod[#2,128])*256^2 + #3*256 + #4)/(2^23) + 1}& @@
       Read[stream,{Byte,Byte,Byte,Byte}]

Reading the rest of the package source, we note that the third argument  
corresponds to endianness, with True meaning big-endian, and False,  
little-endian. So, we make the needed changes:

rb[stream_InputStream,Single,bigendian_] :=
   floatvalue @@ {Quotient[#1,128],
       (Mod[#1,128])*2 + Quotient[#2,128],
       ((Mod[#2,128])*256^2 + #3*256 + #4)/(2^23) + 1}& @@
     If[bigendian,Identity,Reverse]@Read[stream,{Byte,Byte,Byte,Byte}]

and, just like that, we have support for little-endian single precision  
numbers (or at least, I hope so--I didn't test this). Do the same for  
double precision numbers and you're finished updating the package.

I hope this fixes your problem so that workarounds are no longer needed.  
However, I still think that using Import to start with rather than trying  
to read the file manually would have been easier, as well as avoiding this  
bizarre problem.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous 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