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

#include <string>
int i = std::stoi(s, nullptr, 2);
var i = int.parse(s, radix: 2);
import "strconv"
i, err := strconv.ParseInt(s, 2, 0)
SysUtils
i := StrToInt('%'+s);
i = int(s, 2)
i = s.to_i(2)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms