如何让 base64 编码器对变量的值进行编码?
How do I get the base64 encoder to encode the value of a variable?
我正在编写一个基本的 base64 编码器只是为了学习。
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)
当我 运行 程序时,它只是在 base 64 中打印出“提示”。我怎样才能让它打印出我键入的任何消息?
base64.bXXencode
将引发一个 TypeError
如果你传递给它一个 str,你需要你的文本的字节版本,即编码的
使用str.encode("utf-8")
并调用b16encode(prompt)
prompt = input("Type your message -> ").encode("utf-8")
bp = int(input("Base 16, 32, or 64? (Type a number) -> "))
if bp == 16:
encoded = base64.b16encode(prompt)
elif bp == 32:
encoded = base64.b32encode(prompt)
elif bp == 64:
encoded = base64.b64encode(prompt)
else:
raise Exception(f"The value {bp} isn't supported")
print(encoded)
您需要使用 encode()
将字符串转换为 bytes-like 对象。
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)
我正在编写一个基本的 base64 编码器只是为了学习。
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)
当我 运行 程序时,它只是在 base 64 中打印出“提示”。我怎样才能让它打印出我键入的任何消息?
base64.bXXencode
将引发一个 TypeError
如果你传递给它一个 str,你需要你的文本的字节版本,即编码的
使用str.encode("utf-8")
并调用b16encode(prompt)
prompt = input("Type your message -> ").encode("utf-8")
bp = int(input("Base 16, 32, or 64? (Type a number) -> "))
if bp == 16:
encoded = base64.b16encode(prompt)
elif bp == 32:
encoded = base64.b32encode(prompt)
elif bp == 64:
encoded = base64.b64encode(prompt)
else:
raise Exception(f"The value {bp} isn't supported")
print(encoded)
您需要使用 encode()
将字符串转换为 bytes-like 对象。
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)