Search code examples
pythondecimalcrccrc32

CRC - Python - How to calculate JAMCRC decimal number from string


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.


Solution

  • 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