From c713c0f25452a4c96f3d3e9721fd6d242f5e5ef7 Mon Sep 17 00:00:00 2001 From: Swayam Mishra Date: Mon, 13 Oct 2025 01:40:50 +0530 Subject: [PATCH 1/2] docs: Create entry for binascii.a2b_base64() --- .../terms/a2b-base64/a2b-base64.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md diff --git a/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md b/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md new file mode 100644 index 00000000000..488aa227dc6 --- /dev/null +++ b/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md @@ -0,0 +1,85 @@ +--- +Title: '.a2b_base64()' +Description: 'Decodes Base64 encoded data back into its original binary representation' +Subjects: + - 'Python' +Tags: + - 'Modules' + - 'Methods' + - 'Encoding' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +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. + +## Syntax + +```py +binascii.a2b_base64(string) +``` + +### Parameters + +- `string`: A bytes-like object containing Base64 encoded data. The length must be a multiple of 4 (properly padded). + +The method returns the decoded binary data as a bytes object. + +> **Note:** If the input string is not correctly padded to a multiple of 4, a `binascii.Error` exception is raised. + +## Example + +The following example demonstrates using `.a2b_base64()` to decode a Base64 string: + +```py +import binascii + +# A Base64 encoded string representing "Hello World!" +encoded_string = b'SGVsbG8gV29ybGQh' + +# Decode the Base64 string back to binary +decoded_data = binascii.a2b_base64(encoded_string) + +print(f"Original encoded string: {encoded_string}") +print(f"Decoded binary data: {decoded_data}") +print(f"Decoded as text: {decoded_data.decode('utf-8')}") +``` + +The output will be: + +``` +Original encoded string: b'SGVsbG8gV29ybGQh' +Decoded binary data: b'Hello World!' +Decoded as text: Hello World! +``` + +## Codebyte Example + +The following example shows how `.a2b_base64()` handles both valid and invalid Base64 input: + +```codebyte/python +import binascii + +# Valid Base64 string for "Codecademy" +encoded = b'Q29kZWNhZGVteQ==' +decoded = binascii.a2b_base64(encoded) + +print(f"Encoded Base64: {encoded}") +print(f"Decoded data: {decoded}") +print(f"Decoded text: {decoded.decode('utf-8')}") + +# Example with proper padding +encoded_with_padding = b'UHl0aG9u' # "Python" in Base64 +decoded_python = binascii.a2b_base64(encoded_with_padding) +print(f"\nAnother example:") +print(f"Encoded: {encoded_with_padding}") +print(f"Decoded: {decoded_python.decode('utf-8')}") + +# Example with invalid padding (will cause an error) +try: + invalid_encoded = b'abc' # Not properly padded + binascii.a2b_base64(invalid_encoded) +except binascii.Error as e: + print(f"\nError with invalid input 'abc': {e}") +``` \ No newline at end of file From e0bcb4d1a25797330c82d5eac1b0ec95bd455540 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 13 Oct 2025 16:15:31 +0530 Subject: [PATCH 2/2] minor fixes --- .../terms/a2b-base64/a2b-base64.md | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md b/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md index 488aa227dc6..74be66a8c7f 100644 --- a/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md +++ b/content/python/concepts/binascii-module/terms/a2b-base64/a2b-base64.md @@ -1,36 +1,40 @@ --- Title: '.a2b_base64()' -Description: 'Decodes Base64 encoded data back into its original binary representation' +Description: 'Decodes base64 encoded data back into its original binary representation.' Subjects: - - 'Python' + - 'Code Foundations' + - 'Computer Science' + - 'Data Science' Tags: - - 'Modules' - - 'Methods' - 'Encoding' + - 'Methods' + - 'Modules' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -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. +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. ## Syntax -```py +```pseudo binascii.a2b_base64(string) ``` -### Parameters +**Parameters:** + +- `string`: A bytes-like object or ASCII string containing base64-encoded data to be decoded. -- `string`: A bytes-like object containing Base64 encoded data. The length must be a multiple of 4 (properly padded). +**Return value:** -The method returns the decoded binary data as a bytes object. +Returns the original binary data as a `bytes` object after decoding the base64 input. > **Note:** If the input string is not correctly padded to a multiple of 4, a `binascii.Error` exception is raised. ## Example -The following example demonstrates using `.a2b_base64()` to decode a Base64 string: +In this example, `.a2b_base64()` decodes a base64 string representing `"Hello World!"` back into binary form: ```py import binascii @@ -56,7 +60,7 @@ Decoded as text: Hello World! ## Codebyte Example -The following example shows how `.a2b_base64()` handles both valid and invalid Base64 input: +The following example shows how `.a2b_base64()` handles both valid and invalid base64 input: ```codebyte/python import binascii @@ -78,8 +82,8 @@ print(f"Decoded: {decoded_python.decode('utf-8')}") # Example with invalid padding (will cause an error) try: - invalid_encoded = b'abc' # Not properly padded - binascii.a2b_base64(invalid_encoded) + invalid_encoded = b'abc' # Not properly padded + binascii.a2b_base64(invalid_encoded) except binascii.Error as e: - print(f"\nError with invalid input 'abc': {e}") -``` \ No newline at end of file + print(f"\nError with invalid input 'abc': {e}") +```