From d86c7654c2b0bafc583117f7b2b0237db2e30105 Mon Sep 17 00:00:00 2001 From: Anishka Khurana <25anishkakhurana@gmail.com> Date: Thu, 9 Oct 2025 17:08:34 +0530 Subject: [PATCH 1/3] Added new entry a2b-base16 --- .../terms/a2b-base16/a2b-base16.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md diff --git a/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md b/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md new file mode 100644 index 00000000000..5417a86043f --- /dev/null +++ b/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md @@ -0,0 +1,73 @@ +# a2b_base16() + +The `a2b_base16()` function converts hexadecimal (base-16) encoded data to binary data. It takes a string containing hexadecimal digits and returns the corresponding bytes object. This function is part of the `binascii` module and is useful for decoding hexadecimal representations back into their original binary form. + +The function accepts both uppercase and lowercase hexadecimal characters (0-9, A-F, a-f) and ignores whitespace characters in the input string. + +## Syntax + +```python +binascii.a2b_base16(string) +``` + +### Parameters + +- `string`: A string or bytes-like object containing hexadecimal digits to be converted to binary. The string must contain an even number of hexadecimal digits. + +### Return Value + +Returns a bytes object containing the binary data decoded from the hexadecimal input. + +### Exceptions + +- Raises `binascii.Error` if the input contains an odd number of hexadecimal digits or contains invalid characters. + +## Example + +The following example demonstrates how `a2b_base16()` converts hexadecimal strings to binary data: + +```python +import binascii + +# Convert hexadecimal string to binary +hex_string = "48656C6C6F" +binary_data = binascii.a2b_base16(hex_string) + +print("Hexadecimal:", hex_string) +print("Binary data:", binary_data) +print("Decoded text:", binary_data.decode('utf-8')) +# Output: +# Hexadecimal: 48656C6C6F +# Binary data: b'Hello' +# Decoded text: Hello +``` + +In this example, the hexadecimal string "48656C6C6F" is converted to binary data, which represents the ASCII text "Hello". + +## Codebyte Example + +Run the following code to see `a2b_base16()` in action: + +```codebyte/python +import binascii + +# Example 1: Convert hexadecimal to binary +hex_data = "50797468" +result = binascii.a2b_base16(hex_data) +print("Hex:", hex_data) +print("Binary:", result) +print("As text:", result.decode('utf-8')) + +# Example 2: Uppercase and lowercase work the same +hex_upper = "414243" +hex_lower = "414243" +print("\nUppercase result:", binascii.a2b_base16(hex_upper)) +print("Lowercase result:", binascii.a2b_base16(hex_lower)) + +# Example 3: Converting numeric data +hex_numbers = "010203040A0B0C" +binary_numbers = binascii.a2b_base16(hex_numbers) +print("\nHex numbers:", hex_numbers) +print("Binary:", binary_numbers) +print("Byte values:", list(binary_numbers)) +``` \ No newline at end of file From 9132bb9cf56c9c408da7d57c374efdc4d87280e0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 10 Oct 2025 15:13:17 +0530 Subject: [PATCH 2/3] fixed some content --- .../terms/a2b-base16/a2b-base16.md | 73 ---------------- .../binascii-module/terms/a2b-hex/a2b-hex.md | 85 +++++++++++++++++++ 2 files changed, 85 insertions(+), 73 deletions(-) delete mode 100644 content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md create mode 100644 content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md diff --git a/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md b/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md deleted file mode 100644 index 5417a86043f..00000000000 --- a/content/python/concepts/binascii-module/terms/a2b-base16/a2b-base16.md +++ /dev/null @@ -1,73 +0,0 @@ -# a2b_base16() - -The `a2b_base16()` function converts hexadecimal (base-16) encoded data to binary data. It takes a string containing hexadecimal digits and returns the corresponding bytes object. This function is part of the `binascii` module and is useful for decoding hexadecimal representations back into their original binary form. - -The function accepts both uppercase and lowercase hexadecimal characters (0-9, A-F, a-f) and ignores whitespace characters in the input string. - -## Syntax - -```python -binascii.a2b_base16(string) -``` - -### Parameters - -- `string`: A string or bytes-like object containing hexadecimal digits to be converted to binary. The string must contain an even number of hexadecimal digits. - -### Return Value - -Returns a bytes object containing the binary data decoded from the hexadecimal input. - -### Exceptions - -- Raises `binascii.Error` if the input contains an odd number of hexadecimal digits or contains invalid characters. - -## Example - -The following example demonstrates how `a2b_base16()` converts hexadecimal strings to binary data: - -```python -import binascii - -# Convert hexadecimal string to binary -hex_string = "48656C6C6F" -binary_data = binascii.a2b_base16(hex_string) - -print("Hexadecimal:", hex_string) -print("Binary data:", binary_data) -print("Decoded text:", binary_data.decode('utf-8')) -# Output: -# Hexadecimal: 48656C6C6F -# Binary data: b'Hello' -# Decoded text: Hello -``` - -In this example, the hexadecimal string "48656C6C6F" is converted to binary data, which represents the ASCII text "Hello". - -## Codebyte Example - -Run the following code to see `a2b_base16()` in action: - -```codebyte/python -import binascii - -# Example 1: Convert hexadecimal to binary -hex_data = "50797468" -result = binascii.a2b_base16(hex_data) -print("Hex:", hex_data) -print("Binary:", result) -print("As text:", result.decode('utf-8')) - -# Example 2: Uppercase and lowercase work the same -hex_upper = "414243" -hex_lower = "414243" -print("\nUppercase result:", binascii.a2b_base16(hex_upper)) -print("Lowercase result:", binascii.a2b_base16(hex_lower)) - -# Example 3: Converting numeric data -hex_numbers = "010203040A0B0C" -binary_numbers = binascii.a2b_base16(hex_numbers) -print("\nHex numbers:", hex_numbers) -print("Binary:", binary_numbers) -print("Byte values:", list(binary_numbers)) -``` \ No newline at end of file diff --git a/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md b/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md new file mode 100644 index 00000000000..58eaf6839ca --- /dev/null +++ b/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md @@ -0,0 +1,85 @@ +--- +Title: '.a2b_hex()' +Description: 'Converts a hexadecimal (base-16) encoded ASCII string into the original binary data (bytes).' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Encoding' + - 'Functions' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`binascii.a2b_hex()`** function in Python decodes a hexadecimal (base-16) encoded ASCII string into its original binary form. It translates pairs of hexadecimal digits into corresponding byte values, effectively reversing the process of hex encoding. + +## Syntax + +```pseudo +binascii.a2b_hex(string) +``` + +**Parameters:** + +- `string`: A bytes or bytearray object containing hexadecimal (base-16) encoded data. Each pair of hex digits represents one byte, so the length must be even. + +**Return value:** + +Returns a bytes object containing the binary data decoded from the given hexadecimal input. + +**Exceptions:** + +Raises `binascii.Error` if the input contains an odd number of hexadecimal digits or contains invalid characters. + +## Example + +In this example, the hexadecimal string `"48656C6C6F"` is decoded into its corresponding binary data and then interpreted as ASCII text: + +```py +import binascii + +# Convert hexadecimal string to binary +hex_string = "48656C6C6F" +binary_data = binascii.a2b_hex(hex_string) + +print("Hexadecimal:", hex_string) +print("Binary data:", binary_data) +print("Decoded text:", binary_data.decode('utf-8')) +``` + +The output of this code is: + +```shell +Hexadecimal: 48656C6C6F +Binary data: b'Hello' +Decoded text: Hello +``` + +## Codebyte Example + +Run the following code to see `.a2b_hex()` in action with different scenarios: + +```codebyte/python +import binascii + +# Example 1: Convert hexadecimal to binary +hex_data = "50797468" +result = binascii.a2b_hex(hex_data) +print("Hex:", hex_data) +print("Binary:", result) +print("As text:", result.decode('utf-8')) + +# Example 2: Uppercase and lowercase work the same +hex_upper = "414243" +hex_lower = "414243" +print("\nUppercase result:", binascii.a2b_hex(hex_upper)) +print("Lowercase result:", binascii.a2b_hex(hex_lower)) + +# Example 3: Converting numeric data +hex_numbers = "010203040A0B0C" +binary_numbers = binascii.a2b_hex(hex_numbers) +print("\nHex numbers:", hex_numbers) +print("Binary:", binary_numbers) +print("Byte values:", list(binary_numbers)) +``` From 8afb6e1ee04bf57e126a964e5e25ab6f794e501f Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Wed, 29 Oct 2025 16:17:40 +0530 Subject: [PATCH 3/3] Update content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md --- .../python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md b/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md index 58eaf6839ca..3c55bfa4667 100644 --- a/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md +++ b/content/python/concepts/binascii-module/terms/a2b-hex/a2b-hex.md @@ -1,6 +1,6 @@ --- Title: '.a2b_hex()' -Description: 'Converts a hexadecimal (base-16) encoded ASCII string into the original binary data (bytes).' +Description: 'Decodes a hexadecimal (base-16) encoded ASCII string into the original binary data (bytes).' Subjects: - 'Computer Science' - 'Data Science'