Delphi Reverse order of bytes

前端 未结 2 542
暖寄归人
暖寄归人 2021-01-20 03:10

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

相关标签:
2条回答
  • 2021-01-20 03:53
    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;
    
    0 讨论(0)
  • 2021-01-20 03:57
    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;
    
    0 讨论(0)
提交回复
热议问题