Fórum Dúvida para converter binário p/ decimal #165781
20/06/2003
0
function BinToInt(Value: string): Integer;
var
i, iValueSize: Integer;
begin
Result := 0;
iValueSize := Length(Value);
for i := iValueSize downto 1 do
if Value = ´1´ then Result := Result + (1 shl (iValueSize - i));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label2.Caption:=´O número decimal é: ´+ IntToStr(BinToInt(Edit1.Text));
end;
[color=blue:e9d9e46746]Gostaria de adicionar o seguinte: se o usuário digitar no Edit1 um número que não é binário, apareça no label2 isto, ´Você não digitou um número binário´. Será que isto é possível com esta função? Não consegui resolver, se alguém puder me ajudar agradeço.[/color:e9d9e46746] :cry:
Ginosam
Curtir tópico
+ 0Posts
22/06/2003
Cassio Marques
procedure TForm1.Button1Click(Sender: TObject);
Var i : Integer;
begin
for i := Length(edit1.text) downto 1 do
If (Copy(edit1.text,i,1) <> ´0´) and (Copy(edit1.text,i,1) <> ´1´) then
begin
label2.Caption := ´O número não é binário´;
exit;
end
else
label2.Caption:=´O número decimal é:´+ IntToStr(BinToInt(Edit1.Text));
end;
Gostei + 0
22/06/2003
Cassio Marques
function BinToInt(Value: string): Integer;
var
i, iValueSize, Contador: Integer;
begin
Result := 0;
iValueSize := Length(Value);
Contador := 1;
for i := iValueSize downto 1 do
Begin
if Copy(Value,i,1) = ´1´ then Result := Result + Contador;
Contador := Contador * 2;
end;
end;
Gostei + 0
22/06/2003
Ginosam
Gostei + 0
23/06/2003
Thaisandrade
function BinToInt(Texto: string): Int64;
var
X,Val: integer;
Aux: Double;
begin
Aux:=0;
for X:= Length(Texto) downto 1 do
begin
if (Texto[X] <> ´0´) and (Texto[X] <> ´1´) then
begin
raise Exception.Create(´Número binário inválido!´);
end;
if Texto[X] = ´1´ then
begin
Aux:= Aux+Potencia(2,Length(Texto)-X);
end;
end;
Val:= StrToInt(FloatToStr(Aux));
Result:= Val;
end;
function Potencia(Base, Expoente: Double): Double;
begin
if Base = 0 then
Result:=0
else
if Base < 0 then
Result:= Exp(Ln(-Base)*Expoente)
else
Result:= Exp(Ln(Base)*Expoente);
end;
Gostei + 0
23/06/2003
Thaisandrade
function BinToInt(Texto: string): Int64;
var
X,Val: integer;
Aux: Double;
begin
Aux:=0;
for X:= Length(Texto) downto 1 do
begin
if (Texto[X] <> ´0´) and (Texto[X] <> ´1´) then
begin
raise Exception.Create(´Número binário inválido!´);
end;
if Texto[X] = ´1´ then
begin
Aux:= Aux+Potencia(2,Length(Texto)-X);
end;
end;
Val:= StrToInt(FloatToStr(Aux));
Result:= Val;
end;
function Potencia(Base, Expoente: Double): Double;
begin
if Base = 0 then
Result:=0
else
if Base < 0 then
Result:= Exp(Ln(-Base)*Expoente)
else
Result:= Exp(Ln(Base)*Expoente);
end;
Gostei + 0
24/06/2003
Renaldo
dicas e macetes
controlando digitação no edit
Isto é a resposta a suas dúvidas.
Gostei + 0
24/06/2003
Ginosam
implementation
{$R *.dfm}
function Potencia(Base, Expoente: Double): Double;
begin
if Base = 0 then
Result:=0
else
if Base < 0 then
Result:= Exp(Ln(-Base)*Expoente)
else
Result:= Exp(Ln(Base)*Expoente);
end;
function BinToInt(Texto: string): Int64;
var
X,Val: integer;
Aux: Double;
begin
Aux:=0;
for X:= Length(Texto) downto 1 do
begin
if (Texto[X] <> ´0´) and (Texto[X] <> ´1´) then
begin
raise Exception.Create(´Número binário inválido!´);
end;
if Texto[X] = ´1´ then
begin
Aux:= Aux+Potencia(2,Length(Texto)-X);
end;
end;
Val:= StrToInt(FloatToStr(Aux));
Result:= Val;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption:=´O número decimal é: ´+ IntToStr(BinToInt(Edit1.Text));
end;
Obrigado, Mano. Ah... só para mostrar o que eu tinha adicionado no meu outro exemplo:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key= VK_NumPad2) or (key= VK_NumPad3) or (key= VK_NumPad4) or
(key= VK_NumPad5) or (key= VK_NumPad6) or (key= VK_NumPad7) or
(key= VK_NumPad8) or (key= VK_NumPad9) then
begin
label2.Caption:=´Digite somente ´0´ e ´1´ ´;
button1.Enabled:=false;
edit1.Text:=´´;
end;
end;
Gostei + 0
24/06/2003
Thaisandrade
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)