Skip to content

Commit b9a64f2

Browse files
authored
Added .crc32() term under binascii module (#7743)
* created crc32.md * minor tweaks ---------
1 parent cef38b9 commit b9a64f2

File tree

1 file changed

+72
-0
lines changed
  • content/python/concepts/binascii-module/terms/crc32

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
Title: '.crc32()'
3+
Description: 'Computes the CRC-32 checksum of binary data using a cyclic redundancy check algorithm.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Encoding'
9+
- 'Error Handling'
10+
- 'Functions'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`.crc32()`** function computes a CRC-32 checksum of the given data using a cyclic redundancy check algorithm. It is commonly used to detect accidental changes in raw data.
17+
18+
## Syntax
19+
20+
```pseudo
21+
binascii.crc32(data, value=0)
22+
```
23+
24+
**Parameters:**
25+
26+
- `data`: A bytes-like object containing the binary data whose CRC-32 checksum is to be computed.
27+
- `value` (optional): An initial CRC value. This can be used to compute cumulative CRCs across multiple data blocks. Default is `0`.
28+
29+
**Return value:**
30+
31+
Returns an integer representing the CRC-32 checksum of the input data.
32+
33+
## Example
34+
35+
In this example, the CRC-32 checksum of a single string of binary data is computed:
36+
37+
```py
38+
import binascii
39+
40+
# Create binary data
41+
binary_data = b"hello world"
42+
43+
# Compute the CRC-32 checksum
44+
checksum = binascii.crc32(binary_data)
45+
46+
print(checksum)
47+
```
48+
49+
This produces the following output :
50+
51+
```shell
52+
222957957
53+
```
54+
55+
The output value is an unsigned 32-bit integer representing the checksum of the input data.
56+
57+
## Codebyte Example
58+
59+
In this example, the CRC-32 checksum is computed incrementally for multiple chunks of data, simulating a real-world file integrity check:
60+
61+
```codebyte/python
62+
import binascii
63+
64+
chunks = [b"Hello ", b"World!", b" CRC32"]
65+
crc = 0 # initial CRC
66+
67+
# Compute CRC incrementally
68+
for chunk in chunks:
69+
crc = binascii.crc32(chunk, crc)
70+
71+
print("Incremental CRC-32 Checksum:", crc)
72+
```

0 commit comments

Comments
 (0)