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?

askedMay 12, 2018 at 10:55
aladar90's user avatar
1
  • Ifx is already a string, then''.join(map(str, x)) is completely unnecessaryCommentedMay 12, 2018 at 11:00

1 Answer1

1

is this what you want? am I missing something?

>>> binary_string = '1011'>>> binary_integer = int(binary_string, 2)>>> binary_integer11>>> 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.

answeredMay 12, 2018 at 10:59
Ali Yılmaz's user avatar
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

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.