Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-07

byte order

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

Hi,

I need to read 2, 4 or 8 bytes in a processor independent way. For example: I have 4 bytes ($12 $34 $56 $78), starting with the most signifikant (like the 68k processor). Reading a DWord under i386 swaps the bytes ($78563412). Is there a fast way/procedure to read a word/dword/qword independent of the processor?
Something like

function GetWordInLeastSignifikantByteOrder(P: PWord): Word;

If not, how can I determine the current processor byte order?

Mattias

Reply by email

Peter Vreman
to Mattias Gaertner
Show quoted text 15 lines
Hi, I need to read 2, 4 or 8 bytes in a processor independent way. For example: I have 4 bytes ($12 $34 $56 $78), starting with the most signifikant (like the 68k processor). Reading a DWord under i386 swaps the bytes ($78563412). Is there a fast way/procedure to read a word/dword/qword independent of the processor? Something like function GetWordInLeastSignifikantByteOrder(P: PWord): Word; If not, how can I determine the current processor byte order?
{$ifdef FPC_BIG_ENDIAN}
{$ifdef FPC_LITTLE_ENDIAN}

Reply by email

Mattias Gaertner
to Peter Vreman
Show quoted text 18 lines
> Hi, > > I need to read 2, 4 or 8 bytes in a processor independent way. For > example: > I have 4 bytes ($12 $34 $56 $78), starting with the most signifikant > (like the 68k processor). Reading a DWord under i386 swaps the bytes > ($78563412). > Is there a fast way/procedure to read a word/dword/qword independent of > the > processor? > Something like > > function GetWordInLeastSignifikantByteOrder(P: PWord): Word; > > If not, how can I determine the current processor byte order? {$ifdef FPC_BIG_ENDIAN} {$ifdef FPC_LITTLE_ENDIAN}

Thanks.

Mattias

Reply by email

Matt Emson
to Mattias Gaertner: I need to read 2, 4 or 8 bytes in a processor independent way. For example: I have 4…

The word you are looking for is 'Big Endian'. Have a google on 'Big Endian' and 'Little Endian' on google.

this is a delphi routine, it may not work in FPC, certainly I would try it first in Delphi compatibility mode, and with Intel style assembler syntax:

function Swap32(value : dword) : dword ; assembler ;
     asm  bswap eax  end ;

for words there is always 'swap' in the system unit.

Matt

Reply by email