unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
//type
// Tschools = record
// schoolid: Integer;
// schoolcode: string;
// schoolname: string;
// end;
type
RTestInfo = record
Age: Integer;
stuname: string;
// schools: array of Tschools;
end;
PtestInfo = ^RtestInfo;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn4: TButton;
btn5: TButton;
btn6: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure btn4Click(Sender: TObject);
procedure btn5Click(Sender: TObject);
procedure btn6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Data: array[0..1024] of Char;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
Test: RtestInfo;
begin
Test.Age := 13;
Test.stuname := '张三ABC';
// SetLength(Test.schools, 3);
// Test.schools[0].schoolid := 1;
// Test.schools[0].schoolcode := 'code1';
// Test.schools[0].schoolname := 'name1';
// Test.schools[1].schoolid := 2;
// Test.schools[1].schoolcode := 'code2';
// Test.schools[1].schoolname := 'name2';
// Test.schools[2].schoolid := 3;
// Test.schools[2].schoolcode := 'code3';
// Test.schools[2].schoolname := 'name3';
Fillchar(Data, SizeOf(Data), #0);
StrMove(Data, @Test, sizeof(RtestInfo));
//数据发送
ShowMessage(IntToStr(Test.Age));
end;
procedure TForm1.btn2Click(Sender: TObject);
var
vvTest: RtestInfo;
begin
vvTest.Age := 11;
StrMove(@vvTest, Data, sizeof(RtestInfo));
ShowMessage(IntToStr(vvTest.Age));
ShowMessage(vvTest.stuname);
// ShowMessage(IntToStr(vvTest.schools[2].schoolid));
end;
procedure TForm1.btn5Click(Sender: TObject);
var
str: string;
ps: PChar;
n: Cardinal;
begin
str := 'ABCDE';
ps := PChar(str);
n := Cardinal(ps);
//n := Cardinal(str); {这行可以代替上面两行}
ShowMessage(IntToStr(n)); {6089432 结果是 Windows 随机管理的}
ShowMessage(PChar(n)); {ABCDE}
ShowMessage(PChar(n + 2)); {BCDE}
ShowMessage(PChar(n + 4)); {CDE}
ShowMessage(PChar(n + 6)); {DE}
ShowMessage(PChar(n + 8)); {E}
end;
procedure TForm1.btn3Click(Sender: TObject);
var
str: string;
n: Cardinal;
pb: PByte;
begin
str := 'ABCDE';
n := Cardinal(str);
ShowMessage(IntToStr(n)); {6089272; 这是我这里的结果, 这是随机的}
pb := PByte(n);
ShowMessage(IntToStr(pb^)); {65}
pb := PByte(n + 2);
ShowMessage(IntToStr(pb^)); {66}
pb := PByte(n + 4);
ShowMessage(IntToStr(pb^)); {67}
end;
procedure TForm1.btn4Click(Sender: TObject);
var
str: string;
n: Cardinal;
pint: PInteger;
begin
str := 'ABCDE';
n := Cardinal(str);
ShowMessage(IntToStr(n)); {6089884 <--- 4571140; 这是我这里的结果, 这是随机的}
pint := PInteger(n);
ShowMessage(IntToStr(pint^)); {4325441 <--- 1145258561}
pint := PInteger(n + 1);
ShowMessage(IntToStr(pint^)); {1124090368 <--- 1162101570}
pint := PInteger(n + 2);
ShowMessage(IntToStr(pint^)); {4390978}
// 上面的第三个程序段的结果或许让你迷惑:
// 第一个结果, 应该和 "ABCD" 有点关系才对啊, 怎么是: 1145258561 ?
// 第二个结果, 应该和 "BCDE" 有点关系才对啊, 怎么是: 1162101570 ?
// 为什么呢? 这当然没错, 听我解释:
// 1145258561 转换成十六进制是: 44434241, 写得清楚一点是: $44 $43 $42 $41; 还记得
// Intel 等当下流行的 CPU 安排数据是倒着的吗?
end;
procedure TForm1.btn6Click(Sender: TObject);
begin
ShowMessage(IntToHex(4325441)); {00420041}
ShowMessage(IntToHex(1124090368)); {43004200}
ShowMessage(IntToHex(4390978)); {00430042}
// Integer 4字节 有符号32位 -2147483648 .. 2147483647
// Cardinal 4字节 无符号32位 0 .. 4294967295
// Byte 1字节 无符号8位 0 .. 255
end;
end.