BinConvertor

unit BinConvertor;

interface

function BinToInt(value: string): Integer;
function IntToBin(value: Longint; digits: Integer): stringoverload;
function IntToBin(value: Longint): stringoverload;

implementation

function BinToInt(value: string): Integer;
var
  i, len: Integer;
begin
  Result :
= 0;
  len :
= Length(value);
  
for i := len downto 1 do begin
    
if value[i] = '1' then Result := Result + (1 shl (len - i));
  
end;
end;

function IntToBin(value: Longint; digits: Integer): stringoverload;
var
  i: Integer;
begin
  Result :
= '';
  
for i := Digits - 1 downto 0 do
  
begin
    
if value and (1 shl i) <> 0 then
      Result :
= Result + '1'
    
else Result := Result + '0';
  
end;
end;

function IntToBin(value: Longint): stringoverload;
var
  i: Integer;
  bin: 
string;
begin
  bin :
= '';
  
for i := 1 to 8 * SizeOf(value) do
  
begin
    
if Odd(value) then bin := '1' + bin
    
else bin := '0' + bin;
    value :
= value shr 1;
  
end;
  Delete(bin, 
18 * ((Pos('1', bin) - 1div 8));
  Result :
= bin;
end;

end.
原文地址:https://www.cnblogs.com/LeoWong/p/2140034.html