我正在尝试使用来自 here 的用 Python 编写的比特币地址验证器:
这段代码给我带来了麻烦:
def decode_base58(bc, length):
n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, 'big')
我知道 n 是一个 int 或一个 long,但都没有名为 to_bytes 的方法,所以我真的不明白这段代码是如何工作的?
有人知道这里出了什么问题吗?我做错了什么,还是这段代码写错了?欢迎所有提示!
最佳答案
Python 2.7 int
和 long
没有 .to_bytes
方法。 Python 3.2 int
有 .to_bytes
方法。
Python 2.x 的解决方法:
>>> length = 10
>>> n = 123456789
>>> ('%%0%dx' % (length << 1) % n).decode('hex')[-length:]
'\x00\x00\x00\x00\x00\x00\x07[\xcd\x15'
关于Python 'long' 对象没有属性 'to_bytes'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003021/