delphi中String,PChar,PByte,Array of Char,Array of Byte 转换
1 vars:string; 2 pc:pchar; 3 pb:pbyte; 4 ac:array[1..100] of char; 5 ab:array[1..100] of byte; 6 i:integer; 7 begin 8 s:='this is a test'; 9 pc:=pchar(s); //string->pchar 10 pb:=pbyte(pc); //pchar->pbyte 11 for i:=1 to length(s) do 12 begin 13 ac[i]:=s[i]; //string->arrary of char 14 ab[i]:=byte(s[i]); //string->arrary of byte 15 end; 16 s:=pc; //pchar->string 17 s:=string(pb); //pbyte->string 18 s:=ac; //arrary of char->string; 19 end;
Convert byte array using the built-in SetString
command
SetString(AnsiStr, PAnsiChar(@ByteArray[0]), Length(ByteArray));
SetString(UnicodeStr, PWideChar(@ByteArray[0]), Length(ByteArray) div 2);