[阅读: 571] 2004-11-19 08:31:57
function ReverseBytes(Value: Word): Word;
begin
Result := ((Value and Word($FF00)) shr BitsPerByte) or ((Value and Word($00FF)) shl BitsPerByte);
end;
//--------------------------------------------------------------------------------------------------
function ReverseBytes(Value: Smallint): Smallint;
begin
Result := ReverseBytes(Word(Value));
end;
//--------------------------------------------------------------------------------------------------
function ReverseBytes(Value: Longword): Longword;
var
I: Integer;
begin
Result := Value and Longword(ByteMask);
Value := Value shr BitsPerByte;
for I := 0 to SizeOf(Longword) - 2 do
begin
Result := (Result shl BitsPerByte) or (Value and Longword(ByteMask));
Value := Value shr BitsPerByte;
end;
end;
//--------------------------------------------------------------------------------------------------
function ReverseBytes(Value: Longint): Longint;
begin
Result := ReverseBytes(Longword(Value));
end;
//--------------------------------------------------------------------------------------------------
{ TODO : find better solution }
function ReverseBytes(Value: Int64): Int64;
var
I: Integer;
begin
Result := Value and ByteMask;
Value := Value shr BitsPerByte;
for I := 0 to SizeOf(Int64) - 2 do
begin
Result := (Result shl BitsPerByte) or (Value and ByteMask);
Value := Value shr BitsPerByte;
end;
end;
//--------------------------------------------------------------------------------------------------
function ReverseBytes(P: Pointer; Count: Integer): Pointer;
var
P1, P2: PByte;
T: Byte;
begin
if (P <> nil) and (Count > 0) then
begin
P1 := P;
P2 := PByte(Integer(P) + Count - 1);
while Integer(P1) < Integer(P2) do
begin
T := P1^;
P1^ := P2^;
P2^ := T;
Inc(P1);
Dec(P2);
end;
end;
Result := P;
end;