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

Sven Barth pascaldragon at googlemail.com
Sat Jun 4 09:32:14 CEST 2016

Am 04.06.2016 00:40 schrieb "Bo Berglund" <bo.berglund at gmail.com>:
> On Sat, 4 Jun 2016 00:05:21 +0200, Sven Barth
> <pascaldragon at googlemail.com> wrote:
> >FPC's NToBE and BEToN routines indeed only convert if necessary. I don't
> >know about the Delphi's Swap routines, but alone from the name I'd say
> >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.
> >
> Yes, but then I would also need to use the NToBE and BEToN functions
> in the FPC code block and my existing code in the D2007 code in order
> for it to work when I run on FPC but in Linux on x86...
> What this does is that it checks system endian on program startup
> (once) and then uses this flag all through to either convert or not.
> Still with my own Swap4() procedure and the built in Swap() function.

FPC has the defines ENDIAN_LITTLE and ENDIAN_BIG (for Delphi you can
implicitly assume that it's little endian, maybe simply define
ENDIAN_LITTLE if FPC is not set). It's better to enable/disable the
swapping code at compile time so that there is no branch in those routines.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160604/4fc8b199/attachment.html>

More information about the fpc-pascal mailing list