Delphi Char、Pchar 、String 相互转换
1、String 与 PChar 转换
1.1 String转化成PChar 2种方式
var
s:string;
p,p1:PChar;
begin
s:=‘Hi Delphi7‘;
p:=PChar(s); //
ShowMessage(p);
p1:=StrAlloc(Length(s)+1);
StrPCopy(p1,s);
ShowMessage(p1);
StrDispose(p1);
end;
1.2 PChar转String 3种方式
var
s,s1,s2:string;
p:PChar;
begin
s:=‘Hi Delphi7‘;
p:=PChar(s); //ShowMessage(p);
s1:=p; //ShowMessage(s1);
s2:=StrPas(p); //ShowMessage(s2);
end;
2、String 与 Char 转换
2.1 Char转String
var
c:array [0..11] of Char;
s:string;
begin
s:=StrPas(@c[0]); //使用StrPas函数获取数组的首地址 ShowMessage(s);
end;
2.2 String 转 Char
var
s:string;
c:array of Char;
i:Integer;
begin
s:=‘Hi Delphi7‘;
SetLength(c,Length(s));
//Move(s[1],c[0],Length(s));//move 或 CopyMemory
CopyMemory(@c[0],PChar(s),Length(s));
for i:=Low(c) to High(c) do
begin
ShowMessage(string(c[i]))
end;
end;
3、Char 与 PChar 转换
3.1 Char 转 PChar
var
c:array [0..11] of Char;
p:PChar;
begin
c:=‘Hi Delphi7‘;
//p:=@c[0];
p:=PChar(@c[0]);
ShowMessage(StrPas(p));
end;
3.2.PChar 转 Char
var
s:string;
p:PChar;
c:array of Char;
i:Integer;
begin
s:=‘Hi Delphi7‘;
p:=PChar(s);
SetLength(c,Length(s));
//Move(p^,c[0],Length(s));//move 或 CopyMemory
CopyMemory(@c[0],p,Length(s));;
for i:=Low(c) to High(c) do
begin
ShowMessage(string(c[i]))
end;
end;
创建时间:2021.01.13 更新时间: