import base64
# 将字符串编码为 base64
def encode_to_base64(input_string):
    # 将字符串转换为字节类型
    input_bytes = input_string.encode('utf-8')
    # 使用 base64 编码
    encoded_bytes = base64.b64encode(input_bytes)
    # 将编码后的字节转换回字符串
    encoded_string = encoded_bytes.decode('utf-8')
    return encoded_string
# 将 base64 解码为字符串
def decode_from_base64(encoded_string):
    # 将字符串转换为字节类型
    encoded_bytes = encoded_string.encode('utf-8')
    # 使用 base64 解码
    decoded_bytes = base64.b64decode(encoded_bytes)
    # 将解码后的字节转换回字符串
    decoded_string = decoded_bytes.decode('utf-8')
    return decoded_string
# 示例用法
original_string = "Hello, World!"
encoded = encode_to_base64(original_string)
print(f"Encoded: {encoded}")
decoded = decode_from_base64(encoded)
print(f"Decoded: {decoded}")base64 模块,该模块提供了对 Base64 编码和解码的支持。encode_to_base64(input_string):将输入的字符串编码为 Base64 格式。base64.b64encode 方法进行编码,最后将编码后的字节转换回字符串。decode_from_base64(encoded_string):将 Base64 编码的字符串解码回原始字符串。base64.b64decode 方法进行解码,最后将解码后的字节转换回字符串。上一篇:python 矩阵运算
下一篇:python keyboard库
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站