unit endian; // Made by David Peddie 20.11.1999 email: drpeddie@hotmail.com { 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. David Peddie drpeddie@hotmail.com } interface function EndianSwap(w : Byte) : Byte; overload; function EndianSwap(w : Word) : Word; overload; function EndianSwap(w : Longword) : Longword; overload; function EndianSwap(w : Shortint) : Shortint; overload; function EndianSwap(w : Smallint) : Smallint; overload; function EndianSwap(w : Longint) : Longint; overload; function EndianSwap(w : Int64) : Int64; overload; implementation function EndianSwap(w : Byte) : Byte; overload; // unsigned 8 bit begin result := w; end; function EndianSwap(w : Word) : Word; overload; // unsigned 16 bit begin result := swap(w); end; function EndianSwap(w : Longword) : Longword; overload; // unsigned 32 bit var a : Longword; b : Longword; begin a := (Longword(swap(w shr 16)) and $ffff) shl 0; b := (Longword(swap(w shr 0)) and $ffff) shl 16; result := a or b; end; function EndianSwap(w : Shortint) : Shortint; overload; // signed 8 bit begin result := w; end; function EndianSwap(w : Smallint) : Smallint; overload; // signed 16 bit begin result := swap(w); end; function EndianSwap(w : Longint) : Longint; overload; // signed 32 bit var a : Longint; b : Longint; begin a := (Longint(swap(w shr 16)) and $ffff) shl 0; b := (Longint(swap(w shr 0)) and $ffff) shl 16; result := a or b; end; function EndianSwap(w : Int64) : Int64; overload; // signed 64 bit var a : int64; b : int64; c : int64; d : int64; begin a := (Int64(swap(w shr 0)) and $ffff) shl 48; b := (Int64(swap(w shr 16)) and $ffff) shl 32; c := (Int64(swap(w shr 32)) and $ffff) shl 16; d := (Int64(swap(w shr 48)) and $ffff) shl 0; result := a or b or c or d; end; end.