CodeBrewer
CodeBrewer

Reputation: 161

How can I convert a user-input binary value to ASCII characters in Python?

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

Answers (1)

Eevee
Eevee

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

Related Questions