程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 反轉內存的函數

反轉內存的函數

編輯: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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved