Re: How to byte swap an IEEE Float?



Marius Amado Alves <amado.alves@xxxxxxxxxx> writes:

> On 28 Jun 2005, at 14:53, Björn wrote:
>
>> 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?
>
> Simple. Setup an array of bytes with the same representation of the
> float type (use representation clauses and pragma Pack). Read the
> array of bytes. Swap the bytes. Convert to the float type (use
> unchecked conversion).

Marius,

I think you mean 'of the same length as the float type'?

OP doesn't say what processor he gets the BE value from, but assuming
PowerPC you either get Float (4 bytes) for digits <= 6 or Long_Float
(8 bytes) for digits 7 .. 15.

On Intel hardware there's an 80-bit Long_Long_Float (digits 18).

-S
.



Relevant Pages

  • Re: [Announce] Mneson : persistent untyped graphs
    ... Marius Amado Alves wrote: ... > float). ... >> Is it compatible with GPL? ... "Violence is the last refuge of the incompetent" ...
    (comp.lang.ada)
  • Re: How do I rotate a bitmap fast with high quality?
    ... and change float to int using fixed point algorithm. ... I want to know a new algorithm, with high quality and faster speed. ... >though,I could change double type to float type, ...
    (comp.graphics.algorithms)
  • Re: How to byte swap an IEEE Float?
    ... > convert first to a float type of that length, ... All the data in the file are of 32 bits float so that should be ok. ... fairly common task. ... better idea than handling it as a string. ...
    (comp.lang.ada)
  • Re: Float to string conversion in C#
    ... maps api call (which is definitely a float type) into the text ... the textfield doesnt seem to actually ... with the float value clearly inside the text field. ... Having said that, the error message completely threw me, and I dont ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing A Float In WParam or LParam of Windows Message
    ... Based on my experience, m.LParam is of type "IntPtr", however, it should be no problem to convert it into Single if you are sure that this message's lParam is actually float type. ...
    (microsoft.public.dotnet.languages.csharp)