TypeError: ord() expected string of length 1, but int found

TypeError: ord() expected string of length 1, but int found

我尝试在 python 中实现 AES 算法 3.While 实现这个我在 ord() 中发现错误 function.I 只是尝试从 python 转换代码2.3 到 python 3.How 我可以修复错误吗? 我的代码行是:

key = map(ord, key)

提前致谢

您的关键变量已经是一个 bytes 对象(Py2 中的 str)。在Py2中,str是长度为1的序列str,所以需要ord转换为int.

的序列

在 Py3 中,bytes 对象是从 0 到 255 的 int 序列。基本上,在 Python 2 中,您需要 map(ord, key)str 转换为 int 的序列 (list),在 Python 3 中,您根本不需要执行转换,除非你需要改变序列,即使那样,你也可以简单地做 bytearray(key) 来制作原始 bytes.

的可变副本

请注意,Py2.6+ 具有 bytearray 类型,它的行为与 Py3 中的行为相同(ints 的可变序列),因此您可能会写 2/3只需在任何地方使用 bytearray(key) 即可移植代码(而且它会比 map(ord, key) 启动更快)。