[fpc-pascal] Delphi-FPC portable code for endian conversion

Sven Barth pascaldragon at googlemail.com
Sat Jun 4 00:05:21 CEST 2016


Am 03.06.2016 22:08 schrieb "Bo Berglund" <bo.berglund at gmail.com>:
>
> I have a class file implementing interfaces for talking to an embedded
> system and I intend to use this class for both Delphi (on Windows) and
> FPC (on ARM Linux)
> One part of this class deals with transfers of binary data to/from the
> embedded system, which is based on a Motorola MCU and is BigEndian.
> When the class is compiled on Windows it needs to swap 2- and 4-byte
> values to correct for endian differences between Intel and Motorola
> CPU:s.
> I have a working code to do this on Windows, but on FPC I would like
> to use the BEToN() and NToBE functions to convert 16 bit words, 32 bit
> integers and 32 bit floats (single prec) as needed.
> I.e. if running in Delphi on Windows use the Swap and Swap4 functions
> but if running on FPC use the BEToN and NToBE.
> I think I have understood that these functions will convert only if
> the current system is using a different endian than specified by the
> call.

FPC's NToBE and BEToN routines indeed only convert if necessary. I don't
know about the Delphi's Swap routines, bit alone from the name I'd say that
they swap unconditionally (though considering that Delphi runs only on
little endian CPUs that provably won't matter).

> I have tried to figure out a set of {$IFDEF } statements but so far
> with no success.

I'd say that "ifdef fpc" should do the job.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160604/9ba56b20/attachment.html>


More information about the fpc-pascal mailing list