我一直在尝试编写一个带有两个指针(输入和输出)的函数,并以相反的顺序将输入中的字节写入输出。到目前为止,我还没能使它正常工作。
procedure ReverseBytes(Source, Dest: Pointer; Size: Integer);
var
Index: Integer;
begin
Move(Pointer(LongInt(Source) + Index)^, Pointer(LongInt(Dest) + (Size - Index))^ , 1);
end;
任何人都可以建议一个更好的方法来做到这一点。
感谢。
答案 0 :(得分:7)
procedure ReverseBytes(Source, Dest: Pointer; Size: Integer);
begin
Dest := PByte( NativeUInt(Dest) + Size - 1);
while (Size > 0) do
begin
PByte(Dest)^ := PByte(Source)^;
Inc(PByte(Source));
Dec(PByte(Dest));
Dec(Size);
end;
end;
答案 1 :(得分:1)
procedure ReverseBytes(Source, Dest: Pointer; Size: Integer);
var
Index: Integer;
begin
for Index := 0 to Size - 1 do
Move(Pointer(LongInt(Source) + Index)^,
Pointer(LongInt(Dest) + (Size - Index - 1))^ , 1);
end;
procedure TForm2.Button2Click(Sender: TObject);
var
s: AnsiString;
P: Pointer;
begin
s := #0'testreverse';
GetMem(P, Length(s));
ReverseBytes(Pointer(s), P, Length(s));
ShowMessage(PAnsiChar(P)); // esrevertset
FreeMem(P);
end;