Convert Base-2 Binary Number String to Int

Convert base-2 binary number string to int

You use the built-in int() function, and pass it the base of the input number, i.e. 2 for a binary number:

>>> int('11111111', 2)
255

Here is documentation for Python 2, and for Python 3.

How to convert a Binary String to a base 10 integer in Java

You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.

int foo = Integer.parseInt("1001", 2);

Converting base 2 (binary) number to base 10 not working quite well in python

I believe this code will give you the correct result.

    decimal = 0
for i in range(len(binary)):
if binary[i] == '1':
decimal += 2 ** (len(binary) - i - 1)
return decimal

I'm not sure how you calculated your expected output but by my maths it is wrong. the maximum value with this number of bits is 4294967295, and your number takes away 19, leaving you with 4294967276, which i'm pretty sure is the right answer.

Turning a binary in string form to an integer

int('00100', 2)
int('11110', 2)
int('10110', 2)
int('10111', 2)

See Also : Convert base-2 binary number string to int

Convert base 2 number in a binary to an Erlang integer

Use pattern matching:

Bin = <<0:1, 0:1, 0:1, 0:1, 0:1, 1:1, 1:1, 1:1>>,
Size = bit_size(Bin),
<<X:Size>> = Bin.

After that, the variable X contains the integer 7. This works regardless of how many bits the binary contains.


In case you were wondering, it is in fact necessary to bind the bit size to the variable Size before matching. From the section on Bit Syntax Expressions of the Erlang Reference Manual:

Used in a bit string construction, Size is an expression that is to evaluate to an integer.

Used in a bit string matching, Size must be an integer, or a variable bound to an integer.

Convert binary string into integer

Convert.ToInt32(String, Int32) lets you specify the base:

int output = Convert.ToInt32(input, 2);

Binary String to Integer with 'atoi()'

atoi doesn't handle binary numbers, it just interprets them as big decimal numbers. Your problem is that it's too high and you get an integer overflow due to it being interpreted as decimal number.

The solution would be to use stoi, stol or stoll that got added to string in C++11. Call them like

int i = std::stoi("01000101", nullptr, 2);
  • The returned value is the converted int value.
  • The first argument is the std::string you want to convert.
  • The second is a size_t * where it'll save the index of the first non digit character.
  • The third is an int that corresponds to the base that'll be used for conversion..

For information on the functions look at its cppreference page.


Note that there are also pre C++11 functions with nearly the same name, as example: strtol compared to the C++11 stol.

They do work for different bases too, but they don't do the error handling in the same way (they especially lack when no conversion could be done on the given string at all e.g trying to convert "hello" to a string) and you should probably prefer the C++11 versions.

To make my point, passing "Hello" to both strtol and the C++11 stol would lead to:

  • strtol returns 0 and doesn't give you any way to identify it as error,
  • stol from C++11 throws std::invalid_argument and indicates that something is wrong.

Falsely interpreting something like "Hello" as integers might lead to bugs and should be avoided in my opinion.

But for completeness sake a link to its cppreference page too.



Related Topics



Leave a reply



Submit