Friday 24 September 2021

Python: Convert binary string to integer

Using int() method, we can convert a binary string to integer value.

 

Example

int(binary_str, 2)

 

binary_string_to_integer.py

def get_int(binary_str):
    return int(binary_str, 2)

print('"1010" -> ', get_int('1010'))

 

Output

"1010" ->  10

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment