简体   繁体   中英

Delphi Reverse order of bytes

I have been trying to write a function that takes two pointers (an input and an output) and writes the bytes from the input into the output in reverse order. So far I have not been able to make it work correctly.

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

Can anyone please suggest a better way of doing this.

Thanks.

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;
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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM