Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #251 Parse binary digits

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

$i = oct('0b' . $s);
(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 = int(s, 2)
i = s.to_i(2)
let i = i32::from_str_radix(s, 2).expect("Not a binary number!");

New implementation...