string,pchar,array of char之间的转化(转贴)
2010-01-06 16:09
274 查看
string,pchar,array of char之间的转化2009-07-09 10:15设有以下三个变量:
var
s:string;
p:pchar;
a:array[1..20] of char;
那么三者之间的转换如下:
1、字符串到PChar
p:=PChar(s);
2、PChar到字符串
s:=p;
3、PChar到字符数组
StrCopy(@a,p);
4、字符数组到PChar
PChar(@a);
5、字符串与字符数组之间的转换就只有通过PChar来中转了。例如下面这个例子:
procedure TForm1.btn1Click(Sender: TObject);
var
str:array[1..10] of char;
begin
StrCopy(@str,PChar(mmo1.Text));
mmo2.Text:=PChar(@str);
end;
StrCopy, StrCat example
procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: PChar;
begin
GetMem(Buffer,Length(Label1.Caption) + Length(Edit1.Text) + 1);
StrCopy(Buffer, PChar(Label1.Caption));
StrCat(Buffer, PChar(Edit1.Text));
Label1.Caption := Buffer;
Edit1.Clear;
FreeMem(Buffer);
end;
另外当把Pointer型转换为字符串时,用PChar()就行:
procedure TForm1.commReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
sbuf,hexstr:string;
buf:array of byte;
i:integer;
begin
sbuf:='';
sbuf:=PChar(Buffer);
buf:=Buffer;
hexstr:='';
for i:=0 to BufferLength-1 do
hexstr:=hexstr+IntToHex(Buf[i],2)+' ';
if cbHex.Checked then
memLog.Text:=memLog.Text+hexstr
else
memLog.Text:=memLog.Text+sbuf;
end;
var
s:string;
p:pchar;
a:array[1..20] of char;
那么三者之间的转换如下:
1、字符串到PChar
p:=PChar(s);
2、PChar到字符串
s:=p;
3、PChar到字符数组
StrCopy(@a,p);
4、字符数组到PChar
PChar(@a);
5、字符串与字符数组之间的转换就只有通过PChar来中转了。例如下面这个例子:
procedure TForm1.btn1Click(Sender: TObject);
var
str:array[1..10] of char;
begin
StrCopy(@str,PChar(mmo1.Text));
mmo2.Text:=PChar(@str);
end;
StrCopy, StrCat example
procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: PChar;
begin
GetMem(Buffer,Length(Label1.Caption) + Length(Edit1.Text) + 1);
StrCopy(Buffer, PChar(Label1.Caption));
StrCat(Buffer, PChar(Edit1.Text));
Label1.Caption := Buffer;
Edit1.Clear;
FreeMem(Buffer);
end;
另外当把Pointer型转换为字符串时,用PChar()就行:
procedure TForm1.commReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
sbuf,hexstr:string;
buf:array of byte;
i:integer;
begin
sbuf:='';
sbuf:=PChar(Buffer);
buf:=Buffer;
hexstr:='';
for i:=0 to BufferLength-1 do
hexstr:=hexstr+IntToHex(Buf[i],2)+' ';
if cbHex.Checked then
memLog.Text:=memLog.Text+hexstr
else
memLog.Text:=memLog.Text+sbuf;
end;
相关文章推荐
- delphi中string,pchar,array of char,pointer,Pbyte,array of byte之间的转化
- CString char* String 之间的转化
- delphi中String,PChar,PByte,Array of Char,Array of Byte 转换
- string,CString,char*之间的转化
- java byte/char/string/int之间的转化
- string,CString,char*之间的转化
- string、char*、const char*、char [] 之间的相互转化
- CString,string,char*之间的转化(内部的?)
- String,PChar,PByte,Array of Char,Array of Byte 转换
- String,PChar,PByte,Array of Char,Array of Byte 转换
- string,CString,char*之间的转化
- int、char、String之间的相互转化
- String,PChar,PByte,Array of Char,Array of Byte 转换---来自大富翁
- 关于char[]数组,char*指针,std::string*之间的转化
- string, const char*, char 之间的转化
- string,CString,char*之间的转化
- 【语言基础】c++ 基本数据类型与字节数组(string,char [] )之间的转化方法
- CString、string、char*之间的转化
- C#中char[]与string之间的转换;byte[]与string之间的转化
- string,CString,char之间的转化