[fpc-pascal] Byte order modification in fpc on ARM?
Sven Barth
pascaldragon at googlemail.com
Mon Oct 19 17:36:55 CEST 2015
Am 19.10.2015 16:23 schrieb "Bo Berglund" <bo.berglund at gmail.com>:
>
> On Mon, 19 Oct 2015 15:30:36 +0200 (CEST), Michael Van Canneyt
> <michael at freepascal.org> wrote:
>
> >There are compiler defines that give you the endianness.
> >{$IFDEF ENDIAN_BIG}
> >{$IFDEF ENDIAN_LITTLE}
> >
> >And
> >{$IFDEF FPC_LITTLE_ENDIAN}
> >{$IFDEF FPC_BIG_ENDIAN}
>
> Good, that would probably be what I need. Thanks!
> Example:
>
> procedure Swap4 (var Source);
> {Swap the byte order of a 4-byte variable so it matches the PC i86}
> type
> Reverse = packed record
> B1 : Byte;
> I2 : Word;
> B4 : Byte;
> end;
> var
> Bsw : Byte;
> begin
> {$IFDEF FPC_BIG_ENDIAN}
> Bsw := Reverse(Source).B1;
> Reverse(Source).B1 := Reverse(Source).B4;
> Reverse(Source).B4 := Bsw;
> Reverse(Source).I2 := Swap(Reverse(Source).I2);
> {$ENDIF}
> end;
>
> Then all functions calling these functions could stay as they are and
> it will work on both the PC and Raspberry Pi2.
>
> Just have to make sure I don't have the endians reversed...
>
You don't need to implement this yourself. Just use NToBe when transmitting
to the Motorola unit and BeToN when receiving from it. Both functions
provide overloads for the usual sizes (see the documentation link provided
by Jonas).
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151019/de560a38/attachment.html>
More information about the fpc-pascal
mailing list