Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Decimal to binary and vice versa

Title: Decimal to binary and vice versa Question: How to convert binary (base2) to decimal (base10) ? Answer: function IntToBin(Value: LongInt;Size: Integer): String; var i: Integer; begin Result:=''; for i:=Size downto 0 do begin if Value and (1 shl i)0 then Result:=Result+'1'; else Result:=Result+'0'; end; end; function BinToInt(Value: String): LongInt; var i,Size: Integer; begin Result:=0; Size:=Length(Value); for i:=Size downto 0 do begin if Copy(Value,i,1)='1' then Result:=Result+(1 shl i); end; end;