Logo

Programming-Idioms

Extract integer value i from its binary string representation s (in radix 2)
E.g. "1101" -> 13
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
(def i (read-string (str "2r" s)))
#include <string>
int i = std::stoi(s, nullptr, 2);
var i = int.parse(s, radix: 2);
erlang:list_to_integer(S,2).
character (len=:), allocatable :: s
integer :: i  
s = '1101'
read (s,'(B4)') i

  
import "strconv"
i, err := strconv.ParseInt(s, 2, 0)
const i = parseInt(s, 2)
Integer i = Integer.valueOf(s, 2);
import java.math.BigInteger;
int i = new BigInteger(s, 2).intValue();
$i = bindec($s);
uses SysUtils;
i := StrToInt('%'+s);
$i = oct('0b' . $s);
i = int(s, 2)
i = s.to_i(2)
let i = i32::from_str_radix(s, 2).expect("Not a binary number!");