Главная страница | назад





Article #16854: Big-endian and little-endian formated integers

 Question and Answer Database

FAQ1854D.txt   Big-endian and little-endian formated integers
Category   :Compiler
Platform    :All
Product    :All 32 bit  

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;


7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99