I need to convert binary string e.g. 1011 to binary number in python. I used this code but it gives me one more 0 to end
bin(int(''.join(map(str, x)), 2) << 1)
x is string which I want to convert. Could someone help me how to do it?
1 Answer 1
is this what you want? am I missing something?
>>> binary_string = '1011'
>>> binary_integer = int(binary_string, 2)
>>> binary_integer
11
>>> binary_literal = bin(binary_integer)
>>> binary_literal
'0b1011'
If this is not what you want, can you elaborate? what is the input, and what is the desired output? I hope i could help.
answered May 12, 2018 at 10:59
Ali Yılmaz
1,7051 gold badge15 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
zgebac26
Hey Ali, I have a similar problem, and the problem with your solution is that it is still a string. I suppose the initial problem was (like mine), how to transform that binary string to a binary integer. Any ideas?
lang-py
xis already a string, then''.join(map(str, x))is completely unnecessary