我的代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
ABuf : string;
ARandomValue,j : string;
begin
i := 0;
SetLength(ABuf,4);
Randomize;
ARandomValue := Format('%d', [RandomRange(1,0)]);
ARandomValue := '1';
j := Edit2.Text;
for i := 1 to strtoint(j) - 2 do
ARandomValue := ARandomValue + Format('%d', [RandomRange(1,0)]);;
ARandomValue := ARandomValue + '1';
BinToHex(pchar(ABuf),pchar(ARandomValue),strtoint(j)); Edit1.Text := ABuf;
end;总是在获取ABuf时出错,请问这是为什么呢?
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
ABuf : string;
ARandomValue,j : string;
begin
i := 0;
SetLength(ABuf,4);
Randomize;
ARandomValue := Format('%d', [RandomRange(1,0)]);
ARandomValue := '1';
j := Edit2.Text;
for i := 1 to strtoint(j) - 2 do
ARandomValue := ARandomValue + Format('%d', [RandomRange(1,0)]);;
ARandomValue := ARandomValue + '1';
BinToHex(pchar(ABuf),pchar(ARandomValue),strtoint(j)); Edit1.Text := ABuf;
end;总是在获取ABuf时出错,请问这是为什么呢?
BinToHex(@ABuf[0],@ARandomValue[0],strtoint(j));
SetLength(ABuf,4);
改用GetMem(ABuf);最后在
FreeMem(ABuf);
ABuf : PChar;
重新写了个:
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
ABuf : PChar;
Length:integer;
begin Randomize;
try
Length:=StrToInt(Edit2.text);
except
exit;
end;
GetMem(ABuf,Length);
ZeroMemory(ABuf,Length);
for I := 1 to Length - 1 do // Iterate
begin
Byte(ABuf^):=Byte(RandomRange(0,255) and $ff);
inc(ABuf);
end; // for
Dec(aBuf,Length-1);
edit1.Text :=aBuf;
freemem(abuf);
end;