• 0
Sign in to follow this  
HugoTessaro

BinToDec, DecToBin

Question

Olá, vou postar aqui, 2 funções.

Um para converter numero binário para decimal e outra parar converter decimal para binário.

Sou iniciante em delphi, e tive esta duvida, então talvez alguma outra pessoa tambem possa ter. Por ser iniciante, nunca tinha feito uma função tão legal assim :mellow: e creio que esteja certa.

function BinToDec(valor: String): string;
var
decimal: real;
x,y:integer;
begin
decimal:=0;
y:=0;
for x:=Length(valor) DownTo 1 Do
Begin
decimal:=decimal+(StrToFloat(valor[x]))*Exp(y*Ln(2));
y:=y+1;
End;
result:=FloatToStr(decimal);
End;
function DecToBin(valor: integer): string;
var
binario: string;
begin
while valor >= 1 do
begin
binario:=IntToStr(valor mod 2)+binario;
valor:=(valor div 2);
end;
result:=binario;
end;

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this