transformar Binario em decimal

Delphi

07/05/2007

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

Curtidas 0

Respostas

Rodc

Rodc

07/05/2007

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



GOSTEI 0
Fabiano Góes

Fabiano Góes

07/05/2007

Valeu rodc,
vou testar !!!

obrigado !!!


GOSTEI 0
Anderson Mafra

Anderson Mafra

07/05/2007

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)= ´0´ then
    begin
      Result := Result + (1 shl i);
    end;
  end;
end;



se alguem tiver outra ideia será bem vinda tb


GOSTEI 0
Bruno Leandro

Bruno Leandro

07/05/2007

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;
GOSTEI 0
POSTAR