function Dec2Bin(value : Integer; MinBit : Integer) : string;begin result := ''; while (value > 0) do begin if (Trunc(value / 2) * 2 = value) then result := '0' + result else Result := '1' + Result; value := Trunc(value / 2); end; //填满MaxBit位 while (Length(Result) < MinBit) Do Result := '0' + Result;end;
数字都是二进制的,想用二进制显示可以使用IntToBin
function IntToBin(AValue: LongInt; ASize: Integer): String; var I: Integer; begin Result:= EmptyStr; for I:= ASize - 1 downto 0 do begin if AValue and (1 shl I) = 0 then Result:= Result + '0' else Result:= Result + '1'; end; end; procedure TForm1.Button1Click(Sender: TObject); begin Edit1.Text:= IntToBin(3,8) end;
function Dec2Bin(value : Integer; MinBit : Integer) : string;begin result := ''; while (value > 0) do begin if (Trunc(value / 2) * 2 = value) then result := '0' + result else Result := '1' + Result; value := Trunc(value / 2); end; //填满MaxBit位 while (Length(Result) < MinBit) Do Result := '0' + Result;end;
function IntToBin(AValue: LongInt; ASize: Integer): String;
var
I: Integer;
begin
Result:= EmptyStr;
for I:= ASize - 1 downto 0 do
begin
if AValue and (1 shl I) = 0 then
Result:= Result + '0'
else
Result:= Result + '1';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:= IntToBin(3,8)
end;