From 31c63f4caf72937927f79bcd883002216488b765 Mon Sep 17 00:00:00 2001 From: RishitModi Date: Sun, 12 Oct 2025 20:15:47 +0530 Subject: [PATCH 1/3] Updated term entry for b2a-base64.md --- .../terms/b2a-base64/b2a-base64.md | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md diff --git a/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md new file mode 100644 index 00000000000..b1b8c9a341a --- /dev/null +++ b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md @@ -0,0 +1,111 @@ +--- +Title: '.b2a_base64()' +Description: 'Encodes binary data into a Base64 ASCII string representation.' +Subjects: + - 'Python' +Tags: + - 'Methods' + - 'Strings' + - 'Binary' + - 'Encoding' + - 'binascii' +CatalogContent: + - 'learn-python-3' +--- + +## Description + +The **`binascii.b2a_base64()`** method converts binary data into a line of Base64-encoded ASCII characters. +Base64 encoding is commonly used to represent binary data (like images or files) in text-based formats such as email or JSON. + +This function is part of the **`binascii`** module, which provides utilities for converting between binary and ASCII representations. + +--- + +## Syntax + +```python +binascii.b2a_base64(data, *, newline=True) +``` + +### Parameters + +| Parameter | Type | Description | +|------------|------|-------------| +| **data** | `bytes` | The binary data to encode. | +| **newline** | `bool`, optional | If `True` (default), a newline character (`\n`) is appended to the output. Set to `False` to omit it. | + +### Returns +A **`bytes`** object containing the Base64-encoded ASCII representation of the input data. + +--- + +## Example + +```python +import binascii + +# Original binary data +binary_data = b'Hello, Hacktoberfest!' + +# Encode the binary data to Base64 (includes a newline by default) +encoded_data = binascii.b2a_base64(binary_data) + +print(encoded_data) +``` + +**Output:** +``` +b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh\n' +``` + +If you don’t want the trailing newline character, set `newline=False`: + +```python +encoded_data_no_newline = binascii.b2a_base64(binary_data, newline=False) +print(encoded_data_no_newline) +``` + +**Output:** +``` +b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh' +``` + +--- + +## Codebyte Example + +```python +import binascii + +# Binary data to encode +data_to_encode = b'Codecademy Docs!' + +# Perform Base64 encoding +encoded_data = binascii.b2a_base64(data_to_encode) + +# Display the original and encoded data +print(f"Original: {data_to_encode}") +print(f"Encoded: {encoded_data}") +``` + +**Output:** +``` +Original: b'Codecademy Docs!' +Encoded: b'Q29kZWNhZGVteSBEb2NzIQ==\n' +``` + +--- + +## Notes + +- The **Base64** encoding maps each group of 3 bytes of binary data to 4 printable ASCII characters. +- It’s useful when transmitting binary data over text-based protocols like HTTP or SMTP. +- For decoding, you can use the complementary method **`binascii.a2b_base64()`**. + +--- + +## Related Methods + +- [`binascii.a2b_base64()`](https://docs.python.org/3/library/binascii.html#binascii.a2b_base64) — Decodes a Base64-encoded ASCII string back into binary data. +- [`base64.b64encode()`](https://docs.python.org/3/library/base64.html#base64.b64encode) — Higher-level function from the `base64` module for similar functionality. From 22e2b3414337ebe1c97ea87880a59f6aec53afe2 Mon Sep 17 00:00:00 2001 From: RishitModi Date: Sun, 12 Oct 2025 20:20:01 +0530 Subject: [PATCH 2/3] Updated term entry for b2a-base64.md --- .../binascii-module/terms/b2a-base64/b2a-base64.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md index b1b8c9a341a..8e1039c1b18 100644 --- a/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md +++ b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md @@ -96,16 +96,3 @@ Encoded: b'Q29kZWNhZGVteSBEb2NzIQ==\n' ``` --- - -## Notes - -- The **Base64** encoding maps each group of 3 bytes of binary data to 4 printable ASCII characters. -- It’s useful when transmitting binary data over text-based protocols like HTTP or SMTP. -- For decoding, you can use the complementary method **`binascii.a2b_base64()`**. - ---- - -## Related Methods - -- [`binascii.a2b_base64()`](https://docs.python.org/3/library/binascii.html#binascii.a2b_base64) — Decodes a Base64-encoded ASCII string back into binary data. -- [`base64.b64encode()`](https://docs.python.org/3/library/base64.html#base64.b64encode) — Higher-level function from the `base64` module for similar functionality. From c53da32103d9ac6d9ccc45a84c278f8361578aed Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 13 Oct 2025 15:57:22 +0530 Subject: [PATCH 3/3] minor fix --- .../terms/b2a-base64/b2a-base64.md | 78 +++++++------------ 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md index 8e1039c1b18..38f1bbb14c5 100644 --- a/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md +++ b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md @@ -1,98 +1,72 @@ --- Title: '.b2a_base64()' -Description: 'Encodes binary data into a Base64 ASCII string representation.' +Description: 'Converts binary data into a Base64-encoded ASCII string.' Subjects: - - 'Python' + - 'Computer Science' + - 'Data Science' Tags: + - 'Encoding' - 'Methods' - 'Strings' - - 'Binary' - - 'Encoding' - - 'binascii' CatalogContent: - 'learn-python-3' + - 'paths/computer-science' --- -## Description - -The **`binascii.b2a_base64()`** method converts binary data into a line of Base64-encoded ASCII characters. -Base64 encoding is commonly used to represent binary data (like images or files) in text-based formats such as email or JSON. - -This function is part of the **`binascii`** module, which provides utilities for converting between binary and ASCII representations. - ---- +The **`binascii.b2a_base64()`** function encodes binary data into a Base64-encoded ASCII string, making it easier to transmit or store binary content such as images or files in text-based formats. ## Syntax -```python +```pseudo binascii.b2a_base64(data, *, newline=True) ``` -### Parameters +**Parameters:** -| Parameter | Type | Description | -|------------|------|-------------| -| **data** | `bytes` | The binary data to encode. | -| **newline** | `bool`, optional | If `True` (default), a newline character (`\n`) is appended to the output. Set to `False` to omit it. | +- `data`: The binary data to encode. +- `newline` (optional): If set to `False`, omits the trailing newline character. Default is `True`. -### Returns -A **`bytes`** object containing the Base64-encoded ASCII representation of the input data. +**Return value:** ---- +Returns the Base64-encoded version of the input binary data as a bytes object. ## Example -```python +In this example, binary data is encoded into a Base64-encoded bytes object using the default newline setting: + +```py import binascii # Original binary data binary_data = b'Hello, Hacktoberfest!' -# Encode the binary data to Base64 (includes a newline by default) +# Encoding with the default newline character encoded_data = binascii.b2a_base64(binary_data) +# Printing the encoded data print(encoded_data) ``` -**Output:** -``` +The output of this code is: + +```shell b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh\n' ``` -If you don’t want the trailing newline character, set `newline=False`: +> **Note:** If the trailing newline character is not needed, set `newline=False`. -```python -encoded_data_no_newline = binascii.b2a_base64(binary_data, newline=False) -print(encoded_data_no_newline) -``` - -**Output:** -``` -b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh' -``` - ---- ## Codebyte Example -```python -import binascii +In this example, a short text is converted into its Base64-encoded representation, showing both the original and encoded data: -# Binary data to encode +```codebyte/python +import binascii +# The data to be encoded data_to_encode = b'Codecademy Docs!' - # Perform Base64 encoding encoded_data = binascii.b2a_base64(data_to_encode) - -# Display the original and encoded data +# Print the original and encoded data print(f"Original: {data_to_encode}") print(f"Encoded: {encoded_data}") ``` - -**Output:** -``` -Original: b'Codecademy Docs!' -Encoded: b'Q29kZWNhZGVteSBEb2NzIQ==\n' -``` - ----