Skip to content

Commit 3ad83de

Browse files
committed
Append crc32__examples
1 parent 1a3e17e commit 3ad83de

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

crc32__examples/1.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123;
2+
456;
3+
789;

crc32__examples/2.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
143;
2+
456;
3+
799;

crc32__examples/main.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = 'ipetrash'
5+
6+
7+
import zlib
8+
9+
10+
def crc32_from_bytes(data: bytes) -> int:
11+
return zlib.crc32(data) & 0xFFFFFFFF
12+
13+
14+
def crc32_hex_from_bytes(data: bytes) -> str:
15+
return "%08X" % crc32_from_bytes(data)
16+
17+
18+
def crc32_hex_from_file(filename):
19+
with open(filename, 'rb') as f:
20+
buf = f.read()
21+
22+
return crc32_hex_from_bytes(buf)
23+
24+
25+
if __name__ == '__main__':
26+
print(crc32_from_bytes(b'hello-world')) # 2983461467
27+
print(crc32_hex_from_bytes(b'hello-world')) # B1D4025B
28+
print()
29+
print(crc32_hex_from_file('1.csv')) # 7EB0F2B2
30+
print(crc32_hex_from_file('2.csv')) # 5D884C77

0 commit comments

Comments
 (0)