gpt4 book ai didi

python - 弃用警告 : struct integer overflow masking is deprecated

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:10 24 4
gpt4 key购买 nike

我有以下问题。代码如下:

import  binascii, struct
def crc32up(data):
# little endian!!
bin = struct.pack ('<I', binascii.crc32 (data))
return string.upper (binascii.hexlify (bin))

# Generate crc of time code.
#
timecrc_code = crc32up(time_code)

警告是:

 DeprecationWarning: struct integer overflow masking is deprecated
timecrc_code = crc32up(time_code)

是什么导致了这个错误?

最佳答案

您试图打包到您为它们分配的 4 个字节中的值太大:

>>> import struct
>>> n = 2 ** 32
>>> n
4294967296L
>>> struct.pack('<I', n - 1)
'\xff\xff\xff\xff'
>>> struct.pack('<I', n)
__main__:1: DeprecationWarning: struct integer overflow masking is deprecated
'\x00\x00\x00\x00'

较新的 python 版本 (>= 2.6) 还会警告您接受的值:

>>> import struct
>>> struct.pack('<I', -1)
__main__:1: DeprecationWarning: struct integer overflow masking is deprecated
__main__:1: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295
'\xff\xff\xff\xff'

python 告诉您的是,它必须屏蔽值以适合 4 个字节;您可以使用 value & 0xFFFFFFFF 自行执行此操作。

警告仅在 python 程序执行期间发出一次。

请注意,从 2.6 开始,binascii.crc32 值始终是一个有符号 4 字节值,您应该始终使用掩码来打包这些值。这在 2.6 之前并不总是一致的,并且取决于平台。参见 the documentation了解详情。

关于python - 弃用警告 : struct integer overflow masking is deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12339785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com