Delphi帮助中的错误例子

来源:互联网 发布:安全知识网络大赛登录 编辑:程序博客网 时间:2024/06/07 20:41

IntToHex

The following example uses an edit control, a button, and a label on a form. When the button is clicked, the hexadecimal value of each character in the edit control is displayed in the label.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Label1.Caption := '';
  for i := 1 to Length(Edit1.Text) do
  begin
    try
      Label1.Caption := Label1.Caption + IntToHex(Edit1.Text[i],2) + ' ';
    except
      Beep;
    end;
  end;
end;

 

正确应为:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Label1.Caption := '';
  for i := 1 to Length(Edit1.Text) do
  begin
    try
      Label1.Caption := Label1.Caption + IntToHex(StrToInt(Edit1.Text[i]),2) + ' ';
    except
      Beep;
    end;
  end;
end;

原创粉丝点击