3

我一直在尝试编写一个函数,该函数采用两个指针(一个输入和一个输出)并将输入中的字节以相反的顺序写入输出。到目前为止,我还无法使其正常工作。

procedure ReverseBytes(Source, Dest: Pointer; Size: Integer);
var
  Index: Integer;
begin
  Move(Pointer(LongInt(Source) + Index)^, Pointer(LongInt(Dest) + (Size - Index))^ , 1);   
end;

任何人都可以请提出一个更好的方法来做到这一点。

谢谢。

4

2 回答 2

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;
于 2012-10-19T07:22:37.717 回答
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;
于 2012-10-19T02:10:45.563 回答