简体   繁体   中英

CRC32 checksums are different in Zlib Ruby and Python libraries

I'm trying to generate CRC32 checksums in Ruby and Python for the same string and get different results.

Python

from zlib import crc32
x = "SheetJS"
crc32(x)
#=> -1647298270

NodeJS

var CRC32 = require('crc-32');
var x = "SheetJS";
CRC32.str(x); 
#=> -1647298270

Ruby

require 'zlib'
x = "SheetJS"
Zlib::crc32(x)
#=> 2647669026

These are the same values, the difference is in the interpretation. Python and NodeJS interpret crc32 as a signed number, Ruby - as unsigned. Take a look:

In [1]: import struct

In [2]: struct.unpack('i', struct.pack('I', 2647669026))
Out[2]: (-1647298270,)

We are converting 2647669026 to bytes as unsigned integer, and then reading it as a signed one.

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