How to convert binary string to int in Python

2 Answers

0 votes
s = '010101011'

n = int(s, base=2)

print(n)
print(type(n))

 
 
 
'''
run:
 
171
<class 'int'>
 
'''

 



answered Jun 22, 2021 by avibootz
edited Jul 18 by avibootz
0 votes
s = '010101011'
 
i = 0
for x in map(int, s):
    i = i * 2 + x
    
print(i)
print(type(i))

  
  
'''
run:
  
171
<class 'int'>
  
'''

 



answered Jul 18 by avibootz
...