View Full Version : BYTEORDER for floats ?

06-26-2008, 05:48 AM
Dear readers,

I'm working on a linux 64 bits platform, with PV-Wave 9.01, with AMD processor (ie "little endian", as all "PCs")

I need to convert float arrays from and to binary data files, where they are stored in long-endian format.

On 32 bits machines, I used to do BYTEORDER, Data, /Htonl.

This doesn't work on 64-bits machine, and the /Htonl keyword cannot be replaced by /Lswap, which works on 8 bytes.

So, I reverted to :
Data_size = N_ELEMENTS( Data ) * 4
Data = BYTE (Data, 0, Data_size)
Data = REVERSE (REFORM (Data, 4, Data_size/4), 1)
Data = FLOAT (Data, 0, Data_size/4)

Is it the only solution or did I miss some more concise or efficient solution, if ever ?


B. Piguet.

06-26-2008, 12:07 PM
I believe that the BYTEORDER procedure should work on 32-bit floats. Is it possible you mean doubles? If this is the case, take a look at data_convert.pro which is a user library routine (and as such is not supported). Please note that if you intend to run this in runtime mode you will have to compile it first.


06-27-2008, 02:09 AM
I confirm that BYTEORDER , /Htonl does not work for float arrays in my configuration (PV-WAVE Version 9.01 (linux linux64 x86_64)) :

WAVE> a=findgen(3)+0.5
WAVE> print, byte(a, 0, 12)
0 0 0 63 0 0 192 63 0 0 32 64
WAVE> print, byte(a, 0, 12)
63 0 0 0 0 0 0 0 64 32 0 0

The first and third float are reversed as desired, but the second one is replaced by zeroes.
and after this, the first time I try to access the "a" array, I get the message :
% Array has a corrupted descriptor: A.
% Execution halted at $MAIN$ .

uname -a gives :
Linux prktrm4 #1 SMP Tue May 27 13:39:42 EDT 2008 x86_64 Intel(R) Core(TM)2 CPU T5500 @ 1.66GHz GNU/Linux

I can also confirm that the REVERSE(REFORM()) solution does the trick :

WAVE> a=findgen(3)+0.5
WAVE> a=BYTE (a, 0, 12)
WAVE> a=REVERSE (REFORM (a, 4, 3), 1)
WAVE> a=FLOAT(a, 0, 3)
WAVE> PRINT, BYTE(a, 0, 12)
63 0 0 0 63 192 0 0 64 32 0 0

The REVERSE(REFORM()) solution is a good one, it is the one used by data_convert.pro. I only fear that it might be slower than BYTEORDER, but I must confess that I haven't timed it.

When I realised that BYTEORDER didn't work for me, before trying the REVERSE(REFORM()) solution, I wondered if READU/WRITEU had specific keywords (such as /swap2, /swap4, /swap8), since this operation is mostly needed for I/O.
I didn't find them, that's why I asked here.



07-08-2008, 12:54 PM
>I confirm that BYTEORDER , /Htonl does not work for float arrays in my
>configuration (PV-WAVE Version 9.01 (linux linux64 x86_64))

This behavior is actually documented in the Discussion section of the of documentation for BYTEORDER. Unfortunately, there is no keyword alternative that will help you, and thus the work-around described above is the only option.

Change Request # 017074 exists regarding fixing BYTEORDER to handle floats on 64-bit platforms (running 64-bit PV-WAVE)