- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

Big-endian and little-endian formated integers

Question:

How can I create big-endian and little-endian formated integers?

Answer:

The big-endian and little-endian format simply swaps the HiByte

and LoBytes and HiWords and LoWords of a given integer or word value.

The following demonstrates swapping the values of a given word or

longint. Calling the Endian functions a second time reverts the values

back to their original format.



function EndianWord(w : word) : word;

begin

  result := swap(w);

end;



function EndianInt(i : integer) : integer;

begin

  result := swap(i);

end;



function EndianLong(L : longint) : longint;

begin

  result := swap(L shr 16) or

           (longint(swap(L and $ffff)) shl 16);

end;



procedure TForm1.Button1Click(Sender: TObject);

var

  w : word;

  i : integer;

  l : LongInt;

begin

  w := 10;

  i := -10;

  l := 1024;

  w := EndianWord(w);

  i := EndianInt(i);

  l := EndianLong(l);

 {again!}

  w := EndianWord(w);

  i := EndianInt(i);

  l := EndianLong(l);

  Memo1.Lines.Add(IntToStr(w));

  Memo1.Lines.Add(IntToStr(i));

  Memo1.Lines.Add(IntToStr(l));

end;




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile