🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The base64.b64decode function in Python's base64 module decodes Base64-encoded data back into binary data. This function is useful for decoding data that has been encoded in Base64 format, such as when you need to handle data that has been transmitted over text-based protocols like HTTP.
Table of Contents
- Introduction
base64.b64decodeFunction Syntax- Examples
- Basic Usage
- Decoding a String
- Decoding a File
- Real-World Use Case
- Conclusion
Introduction
The base64.b64decode function is part of the base64 module, which provides functions for encoding and decoding data using Base64, a method for representing binary data in an ASCII string format. Base64 decoding is commonly used for converting encoded data back into its original binary form.
base64.b64decode Function Syntax
Here is how you use the base64.b64decode function:
import base64
decoded_data = base64.b64decode(data)
Parameters:
data: The Base64-encoded data to decode. This can be abytesobject or a string.validate(optional): A flag to validate the input data. IfTrueand the input data is incorrectly padded, it will raise abinascii.Error.
Returns:
- A
bytesobject containing the decoded binary data.
Examples
Basic Usage
Decode Base64-encoded binary data using base64.b64decode.
Example
import base64
encoded_data = b'aGVsbG8gd29ybGQ='
decoded_data = base64.b64decode(encoded_data)
print(f"Decoded data: {decoded_data}")
Output:
Decoded data: b'hello world'
Decoding a String
Decode a Base64-encoded string by first converting it to bytes.
Example
import base64
encoded_string = 'aGVsbG8gd29ybGQ='
decoded_string = base64.b64decode(encoded_string).decode('utf-8')
print(f"Decoded string: {decoded_string}")
Output:
Decoded string: hello world
Decoding a File
Decode the contents of a Base64-encoded file.
Example
import base64
with open('encoded_file.txt', 'rb') as file:
encoded_content = file.read()
decoded_content = base64.b64decode(encoded_content)
with open('decoded_file.bin', 'wb') as decoded_file:
decoded_file.write(decoded_content)
print(f"Decoded file content written to 'decoded_file.bin'")
Output:
Decoded file content written to 'decoded_file.bin'
Real-World Use Case
Receiving Binary Data in JSON
When receiving binary data, such as an image, in a JSON payload, it needs to be Base64 decoded.
Example
import base64
import json
json_payload = '{"image": "iVBORw0KGgoAAAANSUhEUgAA...", "description": "Sample image"}'
payload = json.loads(json_payload)
encoded_image = payload['image']
decoded_image = base64.b64decode(encoded_image)
with open('received_image.png', 'wb') as image_file:
image_file.write(decoded_image)
print(f"Decoded image written to 'received_image.png'")
Output:
Decoded image written to 'received_image.png'
Conclusion
The base64.b64decode function is used for decoding Base64-encoded data back into its original binary form in Python. It provides a way to easily decode data that has been encoded for transmission over text-based protocols or storage in text-based formats. By understanding how to use base64.b64decode, you can handle encoded data more effectively and convert it back to its original form when needed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment