binascii.crc32 results not matching

Fredrik Lundh fredrik at pythonware.com
Sat Dec 10 12:43:59 EST 2005


Larry Bates wrote:

> def CalculateCrc(buffer, crc):

/snip/

> The algorithm looks very much like the source code for
> binascii.crc32 (but I'm not a C programmer).

does your Python version give the right result ?   if so, the following
might be somewhat helpful:

def test1(text, crc=0):

    # larry's code
    result = CalculateCrc(text, crc)
    return hex(result[0])

def test2(text, crc=0):

    import Image

    # using pil's crc32 api
    a = (crc >> 16) ^ 65535
    b = (crc & 65535) ^ 65535
    a, b = Image.core.crc32(text, (a, b))
    a ^= 65535
    b ^= 65535
    return "0x%04X%04XL" % (a, b)

def test(text):
    print test1(text), test2(text)

test("hello")
test("goodbye")

this prints

0xF032519BL 0xF032519BL
0x90E3070AL 0x90E3070AL

no time to sort out the int/long mess for binascii.crc32, but it pro-
bably needs the same tweaking as PIL (which handles the CRC as
two 16-bit numbers, to avoid sign hell).

</F>






More information about the Python-list mailing list