Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 986
There is no spoon Avatar asked Nov 26 '25 15:11

There is no spoon


1 Answers

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;
like image 52
LU RD Avatar answered Nov 29 '25 20:11

LU RD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!