## 反转内存的函数 转

涂孟超

``````//按单字节反转内存的函数
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;

``````

### 涂孟超

OrionBox
2012/06/05
0
0
OpenGL超级宝典笔记——操作像素

OpenGL支持放大，缩小，旋转图像。下面将举例介绍这些像素的操作。下面的例子是从tga文件中读取图片并显示，而且可以通过右键菜单来选择图像的显示模式和保存图片的快照到磁盘命名为screens...

Mario_Q
2013/11/06
0
0

yangbodong22011
2017/11/08
0
0
HBase的rowkey的设计原则

HBase是三维有序存储的，通过rowkey（行键），column key（column family和qualifier）和TimeStamp（时间戳）这个三个维度可以对HBase中的数据进行快速定位。 HBase中rowkey可以唯一标识一行...

2018/04/18
0
0
1-STM32嵌入LUA开发(控制小灯闪耀)

2018/07/16
0
0

19分钟前
1
0

25分钟前
0
0

25分钟前
2
0
https

52分钟前
0
0
Quartz监听器Listerner

5
0