溫馨提示×

python3中base64編碼解碼怎么實現

小億
312
2024-04-03 15:35:56
欄目: 編程語言

在Python3中,可以使用內置的base64模塊來進行base64編碼和解碼操作。下面是一個簡單的示例:

import base64

# 要編碼的字符串
original_string = "Hello, world!"

# 進行base64編碼
encoded_string = base64.b64encode(original_string.encode()).decode()
print("Encoded string:", encoded_string)

# 進行base64解碼
decoded_string = base64.b64decode(encoded_string).decode()
print("Decoded string:", decoded_string)

在上面的示例中,首先將原始字符串編碼為字節(jié)流,然后使用base64.b64encode方法進行編碼。然后再使用base64.b64decode方法進行解碼,并最后將解碼后的字節(jié)流轉換為字符串。

0