📄 main.pas
字号:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Math;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
RichEdit1: TRichEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Memo1: TMemo;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Button2: TButton;
Label8: TLabel;
Edit2: TEdit;
procedure Edit1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure RadioButton2Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Memo1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// 10进制整数 转 2进制整数
function IntToBin(aInt: Int64): String;
var
vFlag: Boolean;
vBit: Shortint;
begin
vFlag := aInt < 0;
repeat
vBit := aInt mod 2;
aInt := aInt div 2;
Result := IntToStr(Abs(vBit)) + Result;
until aInt = 0;
if vFlag then
Result := '-' + Result;
end;
//2进制整数 转 10进制整数
Function BinToInt(Str :string) : Int64;
var
vFlag: Boolean;
aInt : Int64;
i : integer;
begin
if (Str[1] = '-') then
begin
vFlag := True;
Str := Copy(Str, 2, Length(Str)-1);
end
else
vFlag := False;
aInt := 0;
for i := 1 to Length(Str) do
aInt := aInt * 2+ ORD(Str[i]) - 48;
Result := aInt;
if vFlag then
Result := Result * (-1);
end;
//10进制纯小数 转 2进制小数
function DecimalToBin(aDec: Double): String;
var
vDoubleIt: Double;
begin
aDec := Frac(Abs(aDec));
if aDec <> 0 then
begin
vDoubleIt := aDec * 2;
Result := IntToStr(Trunc(vDoubleIt)) + DecimalToBin(vDoubleIt);
end;
end;
//2进制小数 转 10进制纯小数
function BinToDecimal(Str: String): Double;
var
i,j: Integer;
aDec: Double;
begin
j := Pos('.', Str);
if (Str = '') then
begin
Result := 0;
Exit;
end;
Str := copy(Str, j+1, Length(Str)-j -1);
j := 0;
aDec := 0;
for i := 1 to Length(Str) do
begin
dec(j);
aDec := aDec + (Ord(Str[i])-48)*Power(2,j);
end;
Result := aDec;
end;
//10进制有理数 转 2进制有理数
function FloatToBin(aFloat: Double): String;
var
vInteger: Int64;
vDecimal: Double;
begin
vInteger := Trunc(aFloat);
vDecimal := Frac(aFloat);
Result := IntToBin(vInteger) + '.' + DecimalToBin(vDecimal);
end;
//2进制有理数 转 10进制有理数
function BinToFloat(Str: String): Double;
var
vInteger: Int64;
vDecimal: Double;
j: Integer;
begin
j := Pos('.', Str);
if j <> 0 then
begin
vInteger := BinToInt(Copy(Str,1,j-1));
vDecimal := BinToDecimal(Copy(Str,j+1,Length(Str)-j));
end
else
begin
vInteger := BinToInt(Str);
vDecimal := 0;
end;
if vInteger > 0 then
Result := vInteger + vDecimal
else
Result := (Abs(vInteger) + vDecimal) * (-1);
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Memo1.OnChange := nil;
if (Edit1.Text <> '')then
if (Pos('.',Edit1.Text) = 0) then
Memo1.Text := IntToBin(StrToInt(Edit1.Text))
else
Memo1.Text := FloatToBin(StrToFloat(Edit1.Text));
Memo1.OnChange := Memo1Change;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
x: Single;
p: PDWORD;
s: String;
i: Integer;
Y: Integer;
begin
x := StrToFloat(Edit1.Text);
p := @x;
s := IntToBin(p^);
Edit2.Text := IntToHex(p^,8);
while Length(s) < 32 do
s := '0' + s;
s := copy(s,1,1) + ' ' + copy(s,2,8) + ' ' + copy(s,10,23);
RichEdit1.Text := s;
RichEdit1.SelStart := 2;
RichEdit1.SelLength := 8;
RichEdit1.SelAttributes.Color := clRed;
s := copy(s,3,8);
Y:= 0;
for i := 1 to Length(s) do
Y := Y + Trunc(StrToInt(s[i])*Power(2, Length(s)-i));
Label3.Caption := IntToStr(Y)+'('+IntToStr(Y-127)+')';
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
Button1.Visible := True;
Button2.Visible := False;
Label8.Caption := 'Excess 127';
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
Button1.Visible := False;
Button2.Visible := True;
Label8.Caption := 'Excess 1023';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
x: Double;
p: PDWORD;
s: String;
i: Integer;
Y: Integer;
begin
x := StrToFloat(Edit1.Text);
p := @x;
s := IntToBin(p^);
while Length(s) < 32 do
s := '0' + s;
Edit2.Text := IntToHex(p^,8);
Inc(p);
s := IntToBin(p^)+s;
Edit2.Text := IntToHex(p^,8) + Edit2.Text;
while Length(s) < 64 do
s := '0' + s;
s := copy(s,1,1) + ' ' + copy(s,2,11) + ' ' + copy(s,13,52);
RichEdit1.Text := s;
RichEdit1.SelStart := 2;
RichEdit1.SelLength := 11;
RichEdit1.SelAttributes.Color := clRed;
s := copy(s,3,11);
Y:= 0;
for i := 1 to Length(s) do
Y := Y + Trunc(StrToInt(s[i])*Power(2, Length(s)-i));
Label3.Caption := IntToStr(Y)+'('+IntToStr(Y-1023)+')';
end;
procedure TForm1.Memo1Change(Sender: TObject);
var
Str: String;
i: Integer;
begin
Edit1.OnChange := nil;
Str := '';
for i := 0 to Memo1.Lines.Count - 1 do
Str := Str + Memo1.Lines.Strings[i];
Edit1.Text :=FloatToStr(BinToFloat(Str));
Edit1.OnChange := Edit1Change;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RichEdit1.SelStart := 2;
RichEdit1.SelLength := 8;
RichEdit1.SelAttributes.Color := clRed;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -