1

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
  • If x is already a string, then ''.join(map(str, x)) is completely unnecessary Commented May 12, 2018 at 11:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.