HanDs
NO.2

[Delphi文章] 反转内存的函数 





学习中请遵循国家相关法律法规,黑客不作恶。没有网络安全就没有国家安全

本站需要登陆后才能查看

//按单字节反转内存的函数  
procedure ReverseMemory(P: PByte; Len: Integer); overload; 
var 
 B: Byte; 
 P2: PByte; 
begin 
 P2 := P; 
 Inc(P2, Len - 1); 
 while P < P2 do 
 begin 
  B := P^; 
  P^ := P2^; 
  P2^ := B; 
  Inc(P); 
  Dec(P2); 
 end; 
end; 
 
//按双字节反转内存的函数 
procedure ReverseMemory(P: PWord; Len: Integer); overload; 
var 
 W: 
Word; 
 P2: PWord; 
begin 
 P2 := P; 
 Inc(P2, Len div 2 - 1); 
 while Integer(P) < Integer(P2) do 
 begin 
  W := P^; 
  P^ := P2^; 
  P2^ := W; 
  Inc(P); 
  Dec(P2); 
 end; 
end; 
 
//按四字节反转内存的函数 
procedure ReverseMemory(P: PInteger; Len: Integer); overload; 
var 
 I: Integer; 
 P2: PInteger; 
begin 
 P2 := P; 
 Inc(P2, Len div 4 - 1); 
 while Integer(P) < Integer(P2) do 
 begin 
  I := P^; 
  P^ := P2^; 
  P2^ := I; 
  Inc(P); 
  Dec(P2); 
 end; 
end; 
 
//调用测试 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 bs: TBytes; 
 i: Integer; 
begin 
 SetLength(bs, 8); 
 for i := 0 to Length(bs) - 1 do bs[i] := i + 49; 
 ShowMessage(StringOf(bs)); {12345678} 
 
 for i := 0 to Length(bs) - 1 do bs[i] := i + 49; 
 ReverseMemory(PByte(bs), Length(bs)); 
 ShowMessage(StringOf(bs)); {87654321} 
 
 for i := 0 to Length(bs) - 1 do bs[i] := i + 49; 
 ReverseMemory(PWord(bs), Length(bs)); 
 ShowMessage(StringOf(bs)); {78563412} 
 
 for i := 0 to Length(bs) - 1 do bs[i] := i + 49; 
 ReverseMemory(PInteger(bs), Length(bs)); 
 ShowMessage(StringOf(bs)); {56781234} 
end; 


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
反转 内存 函数
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组