Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #251 Parse binary digits

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

#include <string>
int i = std::stoi(s, nullptr, 2);
var i = int.parse(s, radix: 2);
character (len=:), allocatable :: s
integer :: i  
s = '1101'
read (s,'(B4)') i

  
import "strconv"
i, err := strconv.ParseInt(s, 2, 0)
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...
programming-idioms.org