Python 3 CRC32 Calculation for Files (Unsigned)
Here's a Python 3 code snippet for calculating an unsigned CRC32 value for a file:
import binascii
def calculate_crc32(filename):
crc = 0xFFFFFFFF
with open(filename, 'rb') as f:
data = f.read()
crc = binascii.crc32(data, crc) & 0xFFFFFFFF
return crc
To use this function, simply pass the filename of the file you want to calculate the CRC32 value for:
filename = 'example.txt'
crc32 = calculate_crc32(filename)
print(hex(crc32)) # prints the CRC32 value in hexadecimal format
Explanation:
The binascii.crc32 function returns a signed 32-bit integer. We use a bitwise AND operation with 0xFFFFFFFF to convert it to an unsigned value, ensuring accurate CRC32 calculation for your files.
原文地址: https://www.cveoy.top/t/topic/nRTF 著作权归作者所有。请勿转载和采集!