Reputation: 161
If the user inputs a binary number in the form of "00110101" or the like, how do I get Python to treat that as the binary value? It must be in this form. The user won't put '0b' before the input. I ultimately want to convert it to an ascii character, but there seems to be nothing about this specific conversion. Just reading it in as binary and converting it to an integer should be enough to get me going if nothing else.
Upvotes: 2
Views: 209
Reputation: 48546
The int
function takes a second argument, the base to use. So:
>>> int("00110101", 2)
53
Then chr
(or unichr
) will turn that into a character.
Upvotes: 5