transformar Binario em decimal

07/05/2007

0

galera,
preciso transformar numeros Binarios em Decimal, até tenho um metodo em Delphi pra isso mais preciso fazer em C++.

eis o metodo em Pascal:
function BinToInt(Value: String): LongInt;
{Converte um numero binário em Inteiro}
var
  i,Size: Integer;
begin
  Result := 0;
  Size := Length(Value);
  for i:= Size downto 0 do
  begin
    if Copy(Value,i,1)= ´1´ then
    begin
      Result := Result + (1 shl i);
    end;
  end;
end;



se alguem tiver outra ideia será bem vinda tb


Fabiano Góes

Fabiano Góes

Responder

Posts

08/05/2007

Rodc

int __fastcall BinToHex(AnsiString Value)
{
    if (Value.Length() > 32)
        throw Exception("Valor maior que 32 bytes");

    int retorno = 0;
    for (int x=Value.Length(); x > 0; x--)
     retorno = retorno | (StrToInt(Value.SubString(x, 1)) << Value.Length()-x);

    return retorno;
}



Responder

08/05/2007

Fabiano Góes

Valeu rodc,
vou testar !!!

obrigado !!!


Responder

25/05/2012

Anderson Mafra

galera,
preciso transformar numeros Binarios em Decimal, até tenho um metodo em Delphi pra isso mais preciso fazer em C++.

eis o metodo em Pascal:
[code:1:05a92d2cfe]
function BinToInt(Value: String): LongInt;
{Converte um numero binário em Inteiro}
var
i,Size: Integer;
begin
Result := 0;
Size := Length(Value);
for i:= Size downto 0 do
begin
if Copy(Value,i,1)= ´0´ then
begin
Result := Result + (1 shl i);
end;
end;
end;
[/code:1:05a92d2cfe]


se alguem tiver outra ideia será bem vinda tb


Responder

25/05/2012

Bruno Leandro

Ola tente desta forma.


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;
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar