python写一个base64转码程序
时间: 2023-03-01 22:59:22 浏览: 85
下面是一个使用 Python 进行 Base64 编码的示例代码:
```
import base64
def base64_encode(data):
return base64.b64encode(data.encode()).decode()
def base64_decode(data):
return base64.b64decode(data.encode()).decode()
if __name__ == "__main__":
original_data = "Hello, World!"
encoded_data = base64_encode(original_data)
decoded_data = base64_decode(encoded_data)
print("原始数据:", original_data)
print("编码后的数据:", encoded_data)
print("解码后的数据:", decoded_data)
```
输出结果如下:
```
原始数据: Hello, World!
编码后的数据: SGVsbG8sIFdvcmxkIQ==
解码后的数据: Hello, World!
```
相关问题
用python写一个base64解码的脚本
可以使用Python内置的base64模块来解码base64编码的字符串,以下是一个示例代码:
```python
import base64
encoded_str = "SGVsbG8gV29ybGQh"
decoded_str = base64.b64decode(encoded_str).decode('utf-8')
print(decoded_str)
```
输出结果为:
```
Hello World!
```
这段代码将base64编码的字符串`"SGVsbG8gV29ybGQh"`解码为`"Hello World!"`。
用python 写一个解base64的函数
好的,这个问题我可以回答。以下是一个简单的Python函数来解码Base64字符串:
```
import base64
def decode_base64(encoded_string):
decoded_bytes = base64.b64decode(encoded_string)
decoded_string = decoded_bytes.decode('utf-8')
return decoded_string
```
只需将base64编码的字符串传递给该函数,它将返回解码后的字符串。
希望我的回答对你有所帮助!