Reputation: 53
I need to calculate CRC-32/JAMCRC decimal number from a string in Python 3. How can I do it? For example, this webpage does it: https://www.crccalc.com/ - for 'hello-world' it prints 1311505828. I would like to have script which does exactly the same calculation. I've already tried to use zlib.crc32 and binascii.crc32 but their results for 'hello-world' is 2983461467.
Upvotes: -1
Views: 1003
Reputation: 1
This code snippet works for me. It calculates CRC32_JAMRC checksum of provided file:
import zlib
with open("some_file", "rb") as f:
crc32_jamcrc = 0xffffffff - zlib.crc32(f.read())
print(hex(crc32_jamcrc))
Upvotes: 0
Reputation: 15384
To compute the CRC-32/JAMCRC you can simply perform the bitwise-not of the standard CRC-32 (in your case, the CRC-32 of 'hello-world' is 2983461467). But you can't simply use the ~
operator, because it works with signed integers (see this answer). Instead, you first need a subtraction:
import zlib
x = b'hello-world'
crc32_jamcrc = int("0b"+"1"*32, 2) - zlib.crc32(x) # 1311505828
Upvotes: 1