中国开发网: 论坛: 程序员情感CBD: 贴子 49422
pcplayer: JCL里的写法:---》
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;

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录