Skip to content

python base64

Base64 is a binary-to-text encoding scheme that represents binary data as a string of ASCII characters. In Python, you can work with Base64 encoding and decoding using the built-in base64 module. Here’s how you can use it:

  1. Encoding Binary Data to Base64:

To encode binary data (e.g., bytes) into Base64, you can use the base64.b64encode() function. Here’s an example:

import base64

binary_data = b'Hello, World!'  # Replace with your binary data
base64_encoded = base64.b64encode(binary_data)

print(base64_encoded.decode('utf-8'))  # Convert to a UTF-8 string for display
  1. Decoding Base64 to Binary Data:

To decode a Base64-encoded string back to binary data, you can use the base64.b64decode() function. Here’s an example:

import base64

base64_string = 'SGVsbG8sIFdvcmxkIQ=='  # Replace with your Base64 string
binary_data = base64.b64decode(base64_string)

print(binary_data.decode('utf-8'))  # Convert to a UTF-8 string for display

Keep in mind that Base64 encoding is commonly used for encoding binary data like images, files, or transmitting binary data over text-based protocols like HTTP or email. It’s not meant for encryption or security; it’s simply a way to represent binary data as text.

Leave a Reply

Your email address will not be published. Required fields are marked *


Enjoy this blog? Please spread the word :)