[fpc-pascal] Re: Endian issues when porting FPC programs from x86 to other platforms

Bo Berglund bo.berglund at gmail.com
Fri Nov 5 11:07:55 CET 2010


On Wed, 03 Nov 2010 10:50:10 +0100, Thomas Schatzl
<tom_at_work at gmx.at> wrote:

>Coming back to the suggestion from jonas:
>
>function BEtoN(const AValue : single) : single;
>type
>  TData = packed record
>    case integer of
>      0 : (s : single);
>      1 : (l : longint);
>  end;
>var
>  d : TData;
>begin
>  d.s := AValue;
>  d.l := system.BEtoN(d.l);
>  result := d.s;
>end;
>
>A BEtoN function for doubles is similar, just replace the single type by
>double, and longint by int64.
>

Hmm,
what would happen if I do this instead:

function BEtoN(const AValue: single): single;
begin
  result := single(BEtoN(longint(AValue)));
end;

I.e. I use typecasting to get the compiler to treat the same 4 bytes
in different ways inside the expression....
Is this legal in FPC?
(I can't test now because I am at work with no FPC system installed)

-- 

Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list