Mega Code Archive

 
Categories / Delphi / Examples
 

Covert string of [zeros and ones] to binary

Question: How can I convert a string of zeros and ones to the integer it represents? Answer: The following example shows how to convert a "BinaryString" to a long integer by using the shl (shift bits left) macro. Example: function BinStringToLongInt(BinString : string) : longint; var i : integer; Num : longint; begin Num := 0; for i := 1 to length(BinString) do if BinString[i] = '1' then Num := (Num shl 1) + 1 else Num := (Num shl 1); Result := Num; end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Lines.Add(IntToStr(BinStringToLongInt('11111111'))); end;