From ce228e42ee0d9abce9ef7b0b0c31625dd54231e5 Mon Sep 17 00:00:00 2001 From: RishitModi Date: Fri, 10 Oct 2025 16:44:04 +0530 Subject: [PATCH 1/2] docs: Created new term-entry for binascii.b2a_base64() --- .../terms/b2a-base64/b2a-base64.md | 64 +++++++++++++++++++ 1 file changed, 64 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..e3aa8f2589e --- /dev/null +++ b/content/python/concepts/binascii-module/terms/b2a-base64/b2a-base64.md @@ -0,0 +1,64 @@ +--- +Title: '.b2a_base64()' +Description: 'Converts binary data to a line of Base64-encoded ASCII characters.' +Subjects: + - 'Python' +Tags: + - 'Methods' + - 'Strings' + - 'Binary' + - 'Encoding' +CatalogContent: + - 'learn-python-3' +--- + +## Syntax + +```python +binascii.b2a_base64(data, *, newline=True) +``` + +- **data**: The binary data to encode. +- **newline** *(optional)*: If set to `False`, omits the trailing newline character. Default is `True`. + +## Example + +```python +import binascii + +# Original binary data +binary_data = b'Hello, Hacktoberfest!' + +# Encoding with the default newline character +encoded_data = binascii.b2a_base64(binary_data) + +# Printing the encoded data +print(encoded_data) +``` + +**Output:** +``` +b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh\n' +``` + +## Codebyte Example + +```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) + +# 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' +``` From 6092edeb9a634fc23ab75caefc20877c4027076f Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 11 Oct 2025 11:47:32 +0530 Subject: [PATCH 2/2] minor tweaks --- .../terms/b2a-base64/b2a-base64.md | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 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 e3aa8f2589e..1a7831f5715 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,29 +1,40 @@ --- Title: '.b2a_base64()' -Description: 'Converts binary data to a line of Base64-encoded ASCII characters.' +Description: 'Converts binary data into a Base64-encoded ASCII string.' Subjects: - - 'Python' + - 'Computer Science' + - 'Data Science' Tags: + - 'Encoding' - 'Methods' - 'Strings' - - 'Binary' - - 'Encoding' CatalogContent: - 'learn-python-3' + - 'paths/computer-science' --- +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) ``` -- **data**: The binary data to encode. -- **newline** *(optional)*: If set to `False`, omits the trailing newline character. Default is `True`. +**Parameters:** + +- `data`: The binary data to encode. +- `newline` (optional): If set to `False`, omits the trailing newline character. Default is `True`. + +**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 @@ -36,14 +47,17 @@ encoded_data = binascii.b2a_base64(binary_data) print(encoded_data) ``` -**Output:** -``` +The output of this code is: + +```shell b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh\n' ``` ## Codebyte Example -```python +In this example, a short text is converted into its Base64-encoded representation, showing both the original and encoded data: + +```codebyte/python import binascii # The data to be encoded @@ -56,9 +70,3 @@ encoded_data = binascii.b2a_base64(data_to_encode) print(f"Original: {data_to_encode}") print(f"Encoded: {encoded_data}") ``` - -**Output:** -``` -Original: b'Codecademy Docs!' -Encoded: b'Q29kZWNhZGVteSBEb2NzIQ==\n' -```