用adler32实现python3字符串的确定性散列

2024-09-28 15:25:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了here以下内容:

Note: To generate the same numeric value across all Python versions and platforms use adler32(data) & 0xffffffff.

我希望将此应用于以下形式的字符串:"S89234IX",但是当我这样做时,我得到:

> zlib.adler32("S89234IX")

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-84eee14d45ae> in <module>()
----> 1 zlib.adler32(campaigns_to_work_with[0])

TypeError: 'str' does not support the buffer interface

对如何将这个函数应用于字符串有什么想法吗?在


Tags: theto字符串herevalueallgeneratenote
1条回答
网友
1楼 · 发布于 2024-09-28 15:25:08

data必须是字节字符串。如果要计算Unicode数据的校验和,则需要将其编码为字节字符串,并且需要确保使用特定的编码。例如,使用UTF-8:

checksum = zlib.adler32("S89234IX".encode('utf-8')) & 0xffffffff

相关问题 更多 >