问个入门级的问题,Pansichar和String如何转换???。。在线等,谢谢。。。。。。。。。 ( 积分: 20 )

  • 主题发起人 主题发起人 giszpp
  • 开始时间 开始时间
G

giszpp

Unregistered / Unconfirmed
GUEST, unregistred user!
如题。。
 
如题。。
 
PChar(你的字符串变量)
 
强制类型转换
pc:Pchar;
s:string;

pc := PChar(s);
s := String(pc);
 
直接赋就是,如下:
var
tmp : PAnsiChar;
str : String;
begin
GetMem( tmp, 5 );
tmp := '1234' + #0;
str := tmp;
Showmessage( str );
end;
 
同意楼上,不过要注意:
Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:

Long strings are reference-counted, while PChars are not.
Assigning to a string copies the data, while a PChar is a pointer to memory.
Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.

Situations in which these differences can cause subtle errors are discussed in this section.
 
PAnsiChar 指向 null 结束的 AnsiChar 字符串的指针。
PAnsiChar :=pansichar(string);
 
多人接受答案了。
 
后退
顶部