反转内存的函数
16lz
2021-05-01
反转内存的函数
//按单字节反转内存的函数procedure ReverseMemory(P: PByte; Len: Integer); overload;var B: Byte; P2: PByte;begin P2 := P; Inc(P2, Len - 1); while Integer(P) < Integer(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;
更多相关文章
- 【php基础入门】细说PHP中的函数声明与使用详解(重要)
- ActionScript 3.0 记要(1): 基本语法
- 几个和当前路径相关的新函数
- 将字符串转换成 UTF8 编码的函数
- 学 Win32 汇编[33] - 探讨 Win32 汇编的模块化编程
- 0428作业-函数
- 学 Win32 汇编[18]: 关于压栈(PUSH)与出栈(POP) 之二
- 学 Win32 汇编[19]: 查看二进制等相关函数
- php之函数,匿名函数与回调函数