From: Björn on
> Sure. You know, I meant representation = bits and bytes, implying same
> length. It seems the OP already has the float type (IEEE_Float_32),
> 32-bit = 4-byte long, and the data are floats of this length, only
> different in byte order.
>
> OP, if the data are of a different length, then you have to unchecked
> convert first to a float type of that length, and then normal convert
> to the final type (risking Constraint_Error).

All the data in the file are of 32 bits float so that should be ok.
The target for the values is at the moment x86 (although I do expect it
to work on PowerPC as well so I check System.Default_Bit_Order before
doing any byte swapping). My dirty workaround for the moment was to
read the data as a string. I was merly wondering which is the
normal/"best" way to deal with this, since I expect it to be a
fairly common task. A packed byte array does however seem like a much
better idea than handling it as a string.

Regards
Björn

From: Stephen Leake on
"Björn" <ssh9614(a)hotmail.com> writes:

> I need to read some float values from file that have been written in
> big-endian byte order from a c-program.

See SAL.Network_Order.Gen_Scalar_64 at
http://www.toadmail.com/~ada_wizard/ada/sal.html for a convenient way
to do this.

> The simple swapping procedure that I have just interchanges the byte
> order of type IEEE_Float_32 to get little-endian.

Ok.

> The problem is that for some values (eg. 33.229000) it is a "NaN"
> when doing IEEE_Float_32'Read and I get a constraint error (invalid
> data) from stream_io when the value is read. How do I get around
> this?

Hmm. Are you saying the original value is _not_ a NaN? If so, then
your swap implementation is faulty; try mine.

--
-- Stephe

From: Marius Amado Alves on

On 28 Jun 2005, at 21:43, Björn wrote:

>> Sure. You know, I meant representation = bits and bytes, implying same
>> length. It seems the OP already has the float type (IEEE_Float_32),
>> 32-bit = 4-byte long, and the data are floats of this length, only
>> different in byte order.
>>
>> OP, if the data are of a different length, then you have to unchecked
>> convert first to a float type of that length, and then normal convert
>> to the final type (risking Constraint_Error).
>
> All the data in the file are of 32 bits float so that should be ok.
> The target for the values is at the moment x86 (although I do expect it
> to work on PowerPC as well so I check System.Default_Bit_Order before
> doing any byte swapping). My dirty workaround for the moment was to
> read the data as a string. I was merly wondering which is the
> normal/"best" way to deal with this, since I expect it to be a
> fairly common task. A packed byte array does however seem like a much
> better idea than handling it as a string.

It is better. Because it's portable. Probably you got away with a
String because on your system a String happens to be a packed array of
bytes, but this is not guaranteed for all systems by the standard.

From: Björn on
> See SAL.Network_Order.Gen_Scalar_64 at
> http://www.toadmail.com/~ada_wizard/ada/sal.html for a convenient way
> to do this.

Thanks! I will check it out.

> Hmm. Are you saying the original value is _not_ a NaN? If so, then
> your swap implementation is faulty; try mine.

No the original value _is_ a NaN when read as a float on x86 (the
subject on this thread was perhaps a bit misleading)

BR
Björn

From: Damien on
Bjýrn a ýcrit :
> I need to read some float values from file that have been written in
> big-endian byte order from a c-program. The simple swapping procedure
> that I have just interchanges the byte order of type IEEE_Float_32 to
> get little-endian. The problem is that for some values (eg. 33.229000)
> it is a "NaN" when doing IEEE_Float_32'Read and I get a constraint
> error (invalid data) from stream_io when the value is read. How do I
> get around this?
>
I had to deal with byte swapping for floats and doubles between Intel
and PPC. This was C++ and I discovered that swapping had to be done in a
neutral form (byte array) before interpretation to float or double.
Initialy I directly swapped floats and doubles, and for certain values
some bits were changed.
So a function with such a signature should never be defined:
function Swap (X : Float) return Float;
It seems that all bit patterns can not be legal floats. I interpreted
this as a normalisation of the float, but this is not sure at all.

So, to swap a float or a double X, do this :
1) convert X to a byte array Y (right size)
2) swap Y from host 1 format to network or archive format
3) send Y
4) read Y
5) swap Y from network or archive format to host 2 format
6) convert Y to X

I don't know if this is related to your problem, but it seems similar,
and it may help.
It was C++, but those issues are language independent.

Damien Carbonne
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Next: Surprise in array concatenation