Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary string into integer

I would like to convert a binary number writen in a String into its integer value.

For example:

string input = "0101"; int output = convert(input); 

output should be equal to 5

like image 825
Christopher Chiche Avatar asked Feb 05 '12 13:02

Christopher Chiche


People also ask

How do you convert binary to integer?

The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n): decimal = d0×20 + d1×21 + d2×22 + ...

Is there a java function that can turn a binary string into an integer?

Convert a Binary String to Int in Java Using Integer. parseInt() The first method is Integer. parseInt() that parses the given string into an int .

How do you convert binary to int in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.

How do you convert a string to a binary number?

To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .


1 Answers

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

int output = Convert.ToInt32(input, 2); 
like image 197
Heinzi Avatar answered Oct 05 '22 07:10

Heinzi