【问题标题】:Python - Why is result of CRC32(np.int64(1)) different to CRC32(np.int32(1))?Python - 为什么 CRC32(np.int64(1)) 的结果与 CRC32(np.int32(1)) 不同?
【发布时间】:2020-12-21 23:35:26
【问题描述】:

Python 中的 CRC32 比较:

>>> zlib.crc32(np.int64(1)) == zlib.crc32(np.int32(1))
False

>>> np.int64(1) == np.int32(1)
True

>>> zlib.crc32(np.int64(1))
2844319735

>>> zlib.crc32(np.int32(1))
2583214201

1 的多项式表达式,无论它的 int64 或 int32 数据类型如何,都应该相同,但它们的 CRC32 结果不同。 我尝试了许多其他数字而不是 1,但 int64 和 int32 结果的 CRC32 仍然不匹配。

非常感谢您对解决这个令人难以置信的令人困惑的问题的任何帮助。

【问题讨论】:

  • CRC 运行在字节流上,而不是数字上。一个是(可能)在做 CRC32(1, 0, 0, 0, 0, 0, 0, 0) 而另一个是做 CRC32(1, 0, 0, 0) 假设小端。
  • 我明白了。稍微扭曲一下我的问题,假设我的系统是 Big Endian,那么 zlib.crc32(np.int64(1)) == zlib.crc32(np.int32(1)) 将返回 True,这是正确的吗?跨度>
  • 没有。 CRC32 对长度敏感。您可以根据需要为其输入任意数量的零并获得(通常)不同的结果,至少在您开始碰撞之前。

标签: python numpy crc32 int64 int32


【解决方案1】:

cbc32 适用于字节。

int32 是 4 个字节,1 是 01 00 00 00

int64是8个字节,1是01 00 00 00 00 00 00 00

>>> zlib.crc32(np.int64(1)) == zlib.crc32(b''.join([np.int32(1), np.int32(0)]))
True

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2015-03-18
    • 2014-12-13
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多