Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the python base64 encoder to encode the value of a variable?

I’m writing a basic base64 encoder just to learn.

import base64
prompt = (input("Type your message -> ")
bp = int(input("Base 16, 32, or 64? (Type a number) -> "))
if bp == 16:
    encoded = base64.b16encode(b'prompt')
elif bp == 32:
     encoded = base64.b32encode(b'prompt')
elif bp == 64:
    encoded = base64.b64encode(prompt)
print(encoded)

When I run the program, it just prints out "prompt" in base 64. How can I get it to print out whatever message I type instead?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use encode() to turn the string into a bytes-like object.

import base64
prompt = input("Type your message -> ")
bp = int(input("Base 16, 32, or 64? (Type a number) -> "))
if bp == 16:
    encoded = base64.b16encode(prompt.encode())
elif bp == 32:
     encoded = base64.b32encode(prompt.encode())
elif bp == 64:
    encoded = base64.b64encode(prompt.encode())
print(encoded)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading