delphi中,char数组、string和Pchar的相互转换

本文介绍了如何在Delphi中进行char数组、string和Pchar之间的转换,包括使用强制类型转换、StrPCopy、StrPas等函数的方法,并详细解释了转换过程中的注意事项和潜在问题。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值