如何使用 Python 将 Base64 解码为 Hex

以下示例将解码 Base64 字符串并重新编码为十六进制。它只需要内置库

这在处理以 Base64 编码的二进制数据时特别有用。

base64_to_hex.py
import base64

def base64_to_hex(base64_string):
    # Decode the base64 string
    decoded_bytes = base64.b64decode(base64_string)
    # Convert the decoded bytes to a hex string
    hex_string = decoded_bytes.hex()
    return hex_string

# Example usage
example_base64 = "SGVsbG8gd29ybGQ="
base64_to_hex(example_base64)

Check out similar posts by category: Python