简体   繁体   中英

Unsigned CRC 32 for Python to match Java's CRC 32?

A Python client communicates with a Java server across an unreliable channel, and they must communicate package corruption via CRC32.

On Java's end, the CRC 32 is unsigned:

//where data is a byte[]
crc.update(data, 8, data.length-8);
long chksum = crc.getValue();

But on Python's end, the CRC 32 is signed:

//where some_segment is a bytearray() of data
crc = zlib.crc32(some_segment)

Is there a way to compute the checksum in Python's crc32 to match that of Java's? In other words, make python's crc32 unsigned?

You can bitwise the result with 0xffffffff to get an unsigned value:

crc = zlib.crc32(some_segment) & 0xffffffff

In Python 3 this is the default, so you only need this on python 2.X, but will work for both versions.

在crc上做& 0xffffffff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM