Skip to content

Commit fe049c5

Browse files
authored
Merge branch 'main' into feat/add-numpy-byteswap-term
2 parents 2ccf6e9 + 73e1467 commit fe049c5

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
Title: 'extend()'
3+
Description: 'Adds multiple elements to the right end of a deque from any iterable.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Algorithms'
9+
- 'Collections'
10+
- 'Data Structures'
11+
- 'Deques'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`extend()`** method adds multiple elements to the right end of a `deque` from any iterable (like a [list](https://www.codecademy.com/resources/docs/python/lists), [tuple](https://www.codecademy.com/resources/docs/python/tuples), or another deque). It modifies the deque in place and returns `None`.
18+
19+
## Syntax
20+
21+
```pseudo
22+
deque.extend(iterable)
23+
```
24+
25+
**Parameters:**
26+
27+
- `iterable`: A sequence or iterable whose elements will be added to the right end of the deque.
28+
29+
**Return value:**
30+
31+
This method does not return any value. It modifies the deque in-place.
32+
33+
## Example
34+
35+
In this example, `extend()` adds elements from a list and another deque to the right end of a deque:
36+
37+
```py
38+
from collections import deque
39+
40+
# Create a deque
41+
dq = deque([1, 2, 3])
42+
43+
# Extend it
44+
dq.extend([4, 5, 6])
45+
print("Extending with list: ", dq)
46+
47+
#Extend it using another deque
48+
dq.extend(deque([7, 8, 9]))
49+
print("Extending with another deque: ", dq)
50+
```
51+
52+
This example results in the following output:
53+
54+
```shell
55+
Extending with list: deque([1, 2, 3, 4, 5, 6])
56+
Extending with another deque: deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
57+
```
58+
59+
## Codebyte Example: Extending a deque with a list and a tuple
60+
61+
In this example, `extend()` adds elements from a list and a tuple, demonstrating its ability to handle different iterables:
62+
63+
```codebyte/python
64+
from collections import deque
65+
66+
# Create a deque with some initial elements
67+
fruits = deque(["apple","banana"])
68+
69+
print("Initital deque:", fruits)
70+
71+
# Add multiple elements to the right using extend()
72+
fruits.extend(["cherry", "date", "elderberry"])
73+
74+
# Extend again using a tuple
75+
fruits.extend(("fig", "grape"))
76+
print("Deque after extending with a tuple:", fruits)
77+
```

0 commit comments

Comments
 (0)