【发布时间】:2015-03-18 08:34:59
【问题描述】:
我在 Python 和 C 中使用 crc32 进行了一些试验,但结果不匹配。
C:
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#define NUM_BYTES 9
int
main(void)
{
uint8_t bytes[NUM_BYTES] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
uint32_t crc = crc32(0L, Z_NULL, 0);
for (int i = 0; i < NUM_BYTES; ++i) {
crc = crc32(crc, bytes, 1);
}
printf("CRC32 value is: %" PRIu32 "\n", crc);
}
给出输出CRC32 value is: 3136421207
Python
In [1]: import zlib
In [2]: int(zlib.crc32("123456789") + 2**32)
Out[2]: 3421780262
在 python 中,我添加 2**32 以“强制转换”为无符号整数。
我在这里错过了什么?
[编辑 1]
现在我已经尝试过
In [8]: crc = 0;
In [9]: for i in xrange(1,10):
...: crc = zlib.crc32(str(i), crc)
...:
In [10]: crc
Out[10]: -873187034
In [11]: crc+2**32
Out[11]: 3421780262
和
int
main(void)
{
uint32_t value = 123456789L;
uint32_t crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, &value, 4);
printf("CRC32 value is: %" PRIu32 "\n", crc);
}
还是不一样的结果。
【问题讨论】: