delphi中,char数组、string和Pchar的相互转换delphi char数组、string和Pchar的相互转换
⼏个函数:
//string->pchar
function StrPCopy(Dest: PChar; const Source: string): PChar;  //SysUtils//
//Pchar的复制
function StrCopy(Dest: PChar; const Source: PChar): PChar;  //SysUtils//
//pchar -> string
function StrPas(const Str: PChar): string;  //SysUtils//
//复制
procedure Move(const Source; var Dest; Count: Integer);  //System//
procedure CopyMemory(Destination:pointer; Source:Pointer; Length:cardinal);  //windows//
/
/内存申请与释放
function StrAlloc(Size: Cardinal): PChar; //SysUtils//
procedure StrDispose(Str: PChar); //SysUtils//
function GetMemory(Size: Integer): Pointer; cdecl;  //System//
function FreeMemory(P: Pointer): Integer; cdecl;  //System//
procedure GetMemo(var P:pointer; Size: Integer);  //System//
procedure FreeMem(var P: Pointer; [Size:Integer]);  //System//
function Copy ( Source : string; StartChar, Count : Integer ) : string;
function Copy ( Source : array; StartIndex, Count : Integer ) : array;
//类型转化
PChar(...)类型转化.
function StrEnd(const Str: PChar): PChar;  //SysUtils//
//Returns a pointer to the end of a null terminated string.
//StrEnd returns a pointer to the null character at the end of Str.
function StrLen(const Str: PChar): Cardinal;  //SysUtils
//Returns number of characters in a string excluding the null terminator.
//StrLen returns the number of characters in Str, not counting the null terminator.
1.string转换成pchar
可以使⽤pchar进⾏强制类型转换,也可以使⽤StrPCopy函数
var
s:string;
p,p1:PChar;
begin
s:='Hello Delphi';
p:=PChar(s);  //字符串转化为pchar
ShowMessage(p);
p1:=StrAlloc(Length(s)+1);
StrPCopy(p1,s);  //把字符串复制到pchar中  //strPCopy把string->pchar  //strPas把pchar->string  ShowMessage(p1);
StrDispose(p1);
end;
2.pchar转换成string
pchar可以直接当string⽤,也可以⽤strpas函数转换⼀下
var
s,s1,s2:string;
p:PChar;
begin
s:='Hello Delphi';
p:=PChar(s);
ShowMessage(p);
s1:=p;  //pchar直接当string使⽤
ShowMessage(s1);
s2:=StrPas(p);  //pchar转化为string    //strPCopy把string->pchar  //strPas把pchar->string  ShowMessage(s2);
end;
3.char数组转换成string
使⽤StrPas函数获取数组的⾸地址
var
c:array [0..11] of Char;
s:string;
begin
c[0]:='H';
c[1]:='e';
c[2]:='l';
c[3]:='l';
c[4]:='o';
c[5]:=' ';
c[6]:='D';
c[7]:='e';
c[8]:='l';
c[9]:='p';
c[10]:='h';
c[11]:='i';
s:=StrPas(@c[0]);  //数组第⼀个元素的地址就是pchar,  pchar转化为string
ShowMessage(s);
//注意
//上⾯的数组是静态数组,上⾯的使⽤⽅法没有问题,甚⾄可以直接
//s := c; //把静态数组赋值组string
/
/静态数组是在定义是就已经在”栈“上分配了空间⼤⼩,在运⾏时这个⼤⼩不能改变
end;
//==========================
//但是动态数组却会出错,
//动态数组的⼤⼩是在运⾏是给定,即,运⾏时在”堆“上分配⼀定的存储空间,同时运⾏时还可以改变其⼤⼩
//动态数组的第⼀个元素为实际数组的开始地址
var
c:array [0..11] of Char;
dc:array  of Char;
s:string;
begin
//静态数组
c[0] := 'H';
c[1] := 'e';
c[2] := 'l';
c[3] := 'l';
c[4] := 'o';
c[5] := ' ';
c[6] := 'D';
c[7] := 'e';
c[8] := 'l';
c[9] := 'p';
c[10] := 'h';
c[11] := 'i';
s := c;    //把静态数组赋值组string
ShowMessage(s);
s := '';
s := StrPas(@c[0]); //数组第⼀个元素的地址就是pchar, pchar转化为string
ShowMessage(s);
//动态数组
SetLength(dc,12);
dc[0] := 'H';
dc[1] := 'e';
dc[2] := 'l';
dc[3] := 'l';
dc[4] := 'o';
dc[5] := ' ';
dc[6] := 'D';
dc[7] := 'e';
dc[8] := 'l';
dc[9] := 'p';
dc[10] := 'h';
dc[11] := 'i';
//s := dc;  //不能这样使⽤, 提⽰错误“Incompatible types:'string' and 'dynamic array'
/
/s := dc[0];  //可以,这是把数组的第⼀个元素赋值给string  //s='H'
s := StrPas(@dc[0]); //数组第⼀个元素的地址就是pchar, pchar转化为string
ShowMessage(s);  //显⽰Hello Delphiasdafdssadfadfgfdsh23$@#$#^^%47^%夺G...  //不是我们想要的Hello Delphi
SetLength(dc,5);
dc[0] := 'H';
dc[1] := 'e';
dc[2] := 'l';
dc[3] := 'l';
dc[4] := 'o';
s := StrPas(@dc[0]);
ShowMessage(s);      //显⽰结果同上(不是想要的结果)
SetLength(dc,20);
dc[0] := 'H';
dc[1] := 'e';
dc[2] := 'l';
dc[3] := 'l';
dc[4] := 'o';
dc[5] := ' ';
dc[6] := 'D';
dc[7] := 'e';
dc[8] := 'l';
dc[9] := 'p';
dc[10] := 'h';
dc[11] := 'i';
dc[12] := #0;
s := StrPas(@dc[0]);
ShowMessage(s);      //显⽰想要的结果
end;
4.string转char数组数组转换成字符串
使⽤move或者copymemory函数
var
s:string;
c:array of Char;
i:Integer;
begin
s:='Hello Delphi';
SetLength(c,Length(s));  //设置数组的长度
//Move(s[1],c[0],Length(s));  //move和CopyMemory都⾏  //procedure system.Move(const Source; var Dest; Count: Integer);  CopyMemory(@c[0],PChar(s),Length(s));
for i:=Low(c) to High(c) do
begin
ShowMessage(string(c[i]))
end;
end;
5.char数组转pchar
var
c:array [0..11] of Char;
p:PChar;
begin
c:='Hello Delphi';
//p:=@c[0];  //char数组可直接当pchar使⽤?
p:=PChar(@c[0]);  //数组的第⼀个元素的地址,转化为pchar指针
ShowMessage(StrPas(p));
end;
6.pchar转char数组
使⽤move或者CopyMemory函数
var
s:string;
p:PChar;
c:array of Char;
i:Integer;
begin
s:='Hello Delphi';
p:=PChar(s);
SetLength(c,Length(s));  //设置数组长度
//Move(p^,c[0],Length(s));//move和CopyMemory都⾏  //把pchar的数据复制到数组
CopyMemory(@c[0],p,Length(s));;  //把pchar的数据复制到数组
for i:=Low(c) to High(c) do
begin
ShowMessage(string(c[i]))
end;
end;
现在讲讲char数组、pchar和string
string和Char数组都是⼀块内存,其中存放连续的字符. string保存具体字符的内存对⽤户是透明的, 由Delphi管理它的分配, 复制和释放, ⽤户不能⼲预(其实也可以, 不过是通过⾮法途径). Char数组就不必说了吧?PChar是⼀个指针, 它的⼤⼩只有32位. 定义时由Delphi⾃动填0 (nil?). 要将PChar作为字符串使⽤的话必须⾃⼰分配内存⽤完必须⾃⼰释放. PChar型字符串由#0表⽰字符串结尾Delphi所提供的相关PChar字符串的操作都是判断#0来决定字符串的结尾的。
因为PChar是指针,所以它能指向任何地⽅(也就是说它不⼀定⾮要指向字符串不可).把⼀个String赋值给PChar只是将String中保存具体字符串的内存的地址给PChar变量. 当然也可以把Char数组第⼀个元素的地址给PChar.
⾄于 哪个占⽤内存⼩, Char数组<PChar(指分配过字符串的)<string(除了具体字符串外还包含字符串长度)
如果空字符串那么PChar<String<array [0..n] of Char
从速度来说毫⽆疑问string最慢, 例如:
作为参数传递(⾮var调⽤时)给过程时string将整个字串的副本传递过去, PChar将指针本⾝的副本传递过去(32位), Char数组和PChar⼀样,传递的是第⼀个元素的地址副本.不过就灵活性来说string最⾼, ⽽且Delphi⽀持的函数最多. 另外可以将String作为Buffer使⽤(因为它当中可以包含字符0).
注:因为string和char数组都是连续的,所以指向string的⾸地址的指针为@s[1],指向char数组的⾸地址的指针为@c[0]。pchar是以#0结尾的,所以很多关于pchar的函数的使⽤的时候要注意,如使⽤StrAlloc函数给pchar分配内存的时候和使⽤StrPCopy函数的时候
7.使⽤StrAlloc函数将⼀个string转换给pchar

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。