Skip to content

Commit b63f52d

Browse files
Docs: Add binascii.a2b_base64() entry (#7725)
* docs: Create entry for binascii.a2b_base64() * minor fixes ---------
1 parent c45e424 commit b63f52d

File tree

1 file changed

+89
-0
lines changed
  • content/python/concepts/binascii-module/terms/a2b-base64

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
Title: '.a2b_base64()'
3+
Description: 'Decodes base64 encoded data back into its original binary representation.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
- 'Data Science'
8+
Tags:
9+
- 'Encoding'
10+
- 'Methods'
11+
- 'Modules'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.a2b_base64()`** method in Python's `binascii` module decodes a string of base64 encoded data. It takes an ASCII string containing base64 encoded data and converts it back into its original binary form.
18+
19+
## Syntax
20+
21+
```pseudo
22+
binascii.a2b_base64(string)
23+
```
24+
25+
**Parameters:**
26+
27+
- `string`: A bytes-like object or ASCII string containing base64-encoded data to be decoded.
28+
29+
**Return value:**
30+
31+
Returns the original binary data as a `bytes` object after decoding the base64 input.
32+
33+
> **Note:** If the input string is not correctly padded to a multiple of 4, a `binascii.Error` exception is raised.
34+
35+
## Example
36+
37+
In this example, `.a2b_base64()` decodes a base64 string representing `"Hello World!"` back into binary form:
38+
39+
```py
40+
import binascii
41+
42+
# A Base64 encoded string representing "Hello World!"
43+
encoded_string = b'SGVsbG8gV29ybGQh'
44+
45+
# Decode the Base64 string back to binary
46+
decoded_data = binascii.a2b_base64(encoded_string)
47+
48+
print(f"Original encoded string: {encoded_string}")
49+
print(f"Decoded binary data: {decoded_data}")
50+
print(f"Decoded as text: {decoded_data.decode('utf-8')}")
51+
```
52+
53+
The output will be:
54+
55+
```
56+
Original encoded string: b'SGVsbG8gV29ybGQh'
57+
Decoded binary data: b'Hello World!'
58+
Decoded as text: Hello World!
59+
```
60+
61+
## Codebyte Example
62+
63+
The following example shows how `.a2b_base64()` handles both valid and invalid base64 input:
64+
65+
```codebyte/python
66+
import binascii
67+
68+
# Valid Base64 string for "Codecademy"
69+
encoded = b'Q29kZWNhZGVteQ=='
70+
decoded = binascii.a2b_base64(encoded)
71+
72+
print(f"Encoded Base64: {encoded}")
73+
print(f"Decoded data: {decoded}")
74+
print(f"Decoded text: {decoded.decode('utf-8')}")
75+
76+
# Example with proper padding
77+
encoded_with_padding = b'UHl0aG9u' # "Python" in Base64
78+
decoded_python = binascii.a2b_base64(encoded_with_padding)
79+
print(f"\nAnother example:")
80+
print(f"Encoded: {encoded_with_padding}")
81+
print(f"Decoded: {decoded_python.decode('utf-8')}")
82+
83+
# Example with invalid padding (will cause an error)
84+
try:
85+
invalid_encoded = b'abc' # Not properly padded
86+
binascii.a2b_base64(invalid_encoded)
87+
except binascii.Error as e:
88+
print(f"\nError with invalid input 'abc': {e}")
89+
```

0 commit comments

Comments
 (0)