Lenguaje de programación Borland Delphi
Convertir número Entero a Binario:
Recursivamente:
function IntToBinRec(valor,digitos:integer):string;
begin
if digitos=0 then
result:=''
else
begin
if (valor AND (1 shl (digitos-1)))>0 then
result:='1'+IntToBinRec(valor,digitos-1)
else
result:='0'+IntToBinRec(valor,digitos-1)
end;
end;
Ejemplo de llamada:
Label1.Caption:=IntToBinRec(207,8);
Otra más eficiente:
function IntToBin(valor,digitos:integer):string;
var
resultado:string;
i:integer;
begin
if digitos>32 then
digitos:=32;
Resultado:='';
i:=0;
while i< digitos do
begin
if ((1 shl i) AND valor)>0 then
Resultado:='1'+resultado
else
Resultado:='0'+resultado;
inc(i);
end;
Result:=resultado;
end;
Ejemplo de llamada:
Label1.Caption:=IntToBin(207,8);
Y otra más:
function IntToBin(Value: LongInt;Size: Integer): String;
var
i: Integer;
begin
Result:='';
for i:=Size-1 downto 0 do
Result:=Result+Chr(48+Integer((Value and (1 shl i))<>0));
end;
Publicado el: 2003-11-07