|
| 1 | +--- |
| 2 | +Title: '.a2b_hex()' |
| 3 | +Description: 'Decodes a hexadecimal (base-16) encoded ASCII string into the original binary data (bytes).' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Encoding' |
| 9 | + - 'Functions' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-python-3' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +binascii.a2b_hex(string) |
| 21 | +``` |
| 22 | + |
| 23 | +**Parameters:** |
| 24 | + |
| 25 | +- `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. |
| 26 | + |
| 27 | +**Return value:** |
| 28 | + |
| 29 | +Returns a bytes object containing the binary data decoded from the given hexadecimal input. |
| 30 | + |
| 31 | +**Exceptions:** |
| 32 | + |
| 33 | +Raises `binascii.Error` if the input contains an odd number of hexadecimal digits or contains invalid characters. |
| 34 | + |
| 35 | +## Example |
| 36 | + |
| 37 | +In this example, the hexadecimal string `"48656C6C6F"` is decoded into its corresponding binary data and then interpreted as ASCII text: |
| 38 | + |
| 39 | +```py |
| 40 | +import binascii |
| 41 | + |
| 42 | +# Convert hexadecimal string to binary |
| 43 | +hex_string = "48656C6C6F" |
| 44 | +binary_data = binascii.a2b_hex(hex_string) |
| 45 | + |
| 46 | +print("Hexadecimal:", hex_string) |
| 47 | +print("Binary data:", binary_data) |
| 48 | +print("Decoded text:", binary_data.decode('utf-8')) |
| 49 | +``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | +Hexadecimal: 48656C6C6F |
| 55 | +Binary data: b'Hello' |
| 56 | +Decoded text: Hello |
| 57 | +``` |
| 58 | + |
| 59 | +## Codebyte Example |
| 60 | + |
| 61 | +Run the following code to see `.a2b_hex()` in action with different scenarios: |
| 62 | + |
| 63 | +```codebyte/python |
| 64 | +import binascii |
| 65 | +
|
| 66 | +# Example 1: Convert hexadecimal to binary |
| 67 | +hex_data = "50797468" |
| 68 | +result = binascii.a2b_hex(hex_data) |
| 69 | +print("Hex:", hex_data) |
| 70 | +print("Binary:", result) |
| 71 | +print("As text:", result.decode('utf-8')) |
| 72 | +
|
| 73 | +# Example 2: Uppercase and lowercase work the same |
| 74 | +hex_upper = "414243" |
| 75 | +hex_lower = "414243" |
| 76 | +print("\nUppercase result:", binascii.a2b_hex(hex_upper)) |
| 77 | +print("Lowercase result:", binascii.a2b_hex(hex_lower)) |
| 78 | +
|
| 79 | +# Example 3: Converting numeric data |
| 80 | +hex_numbers = "010203040A0B0C" |
| 81 | +binary_numbers = binascii.a2b_hex(hex_numbers) |
| 82 | +print("\nHex numbers:", hex_numbers) |
| 83 | +print("Binary:", binary_numbers) |
| 84 | +print("Byte values:", list(binary_numbers)) |
| 85 | +``` |
0 commit comments