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

Bo Berglund bo.berglund at gmail.com
Fri Nov 5 18:25:23 CET 2010


On Fri, 05 Nov 2010 11:07:55 +0100, Bo Berglund
<bo.berglund at gmail.com> wrote:

>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)

Now home and I put this function into a unit I am building and did a
test compile. No errors found...
But when I actually used it to convert a single value I got a runtime
exception...
The suggested function above, however, works fine! :-)
So I will use that to handle the conversions in my program.


-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list