
07.09.2014, 13:44
|
Прохожий
|
|
Регистрация: 18.04.2014
Сообщения: 20
Версия Delphi: Delphi 7
Репутация: 10
|
|
Цитата:
Сообщение от blackstrip
Встроенных функций перевода в двоичную систему - в дельфи нет. Но можно написать свою. Или взять готовую.
Гуглим "delphi превратить число в двоичное", находим, например http://otvet.mail.ru/question/23910910
Берем оттуда функцию, дописываем к ее заголовку "TForm1." и вставляем в код unit1.pas:
Код:
function TForm1.IntToBinary(x: integer): string;
var
str: string;
begin
str:='';
while x>1 do
begin
if x mod 2=0 then str:='0'+str else str:='1'+str;
x:=x div 2;
end;
if x=0 then str:='0'+str else str:='1'+str;
Result:=str;
end;
Пусть на форме у нас Edit1, Edit2 и Button1. В событии по клику для Button1 пишем:
Код:
Edit2.Text := IntToBinary(StrToInt(Edit1.Text));
И еще вверху unit1.pas, там где все заголовки функций юнита описываются (включая заголовок нашей функции обработки нажатия на кнопку "procedure Button1Click(Sender: TObject);"), надо добавить заголовок функции нашей самодельной, но уже без "TForm1.":
Код:
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
function IntToBinary(x: integer): string;
private
{ Private declarations }
public
{ Public declarations }
end;
Итоговый код Unit1.pas (Delphi 7):
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
function IntToBinary(x: integer): string;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.IntToBinary(x: integer): string;
var
str: string;
begin
str:='';
while x>1 do
begin
if x mod 2=0 then str:='0'+str else str:='1'+str;
x:=x div 2;
end;
if x=0 then str:='0'+str else str:='1'+str;
Result:=str;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := IntToBinary(StrToInt(Edit1.Text));
end;
end.
|
Спасибо вам большое)))), очень помогли
|