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:='H