字串转字节数组
一、CopyMemory
var s:PAnsiChar; ary:TArray<Byte>; bt:Byte; begin s:='Form Delphi'; SetLength(ary,Length(string(s))+1); CopyMemory(ary,s,Length(s)); mmo1.Clear; for bt in ary do begin mmo1.Lines.Append(Chr(bt)); end; end;
二、Move
var s:PAnsiChar; pb:PByte; i:Byte; begin s:='From Delphi'; GetMem(pb,Length(string(s))+1); Move(s^,pb^,Length(string(s))+1); mmo1.Clear; { $POINTERMATH ON} for i:=0 to Length(string(s)) do begin mmo1.Lines.Append(Chr(pb[i])); end; { $POINTERMATH OFF} FreeMem(pb,Length(string(s))+1); end;
三、Move
var s:PAnsiChar; pb:PByte; i:Byte; begin s:='From Delphi'; GetMem(pb,Length(string(s))+1); Move(s^,pb^,Length(string(s))+1); mmo1.Clear; i:=0; while (pb^<>Ord(#0)) do begin mmo1.Lines.Append(Chr(pb^)); Inc(pb); Inc(i); end; Dec(pb,i); FreeMem(pb,Length(string(s))+1); end;