我有一个十进制数,我想以base58字符串的形式在屏幕上显示它。我已经有了一些东西:
>>> from base58 import b58encode
>>> b58encode('33')
'4tz'这似乎是正确的,但由于数字小于58,结果的base58字符串不应该仅为一个字符吗?我一定是错过了一些步骤。我认为这是因为我传递的字符串' 33‘实际上不是数字33。
当我传入一个直线整数时,会得到一个错误:
>>> b58encode(33)
TypeError: a bytes-like object is required (also str), not 'int'基本上,我想在base58中编码一个数字,以便它使用尽可能少的字符.
发布于 2018-12-09 18:29:04
base58.b58encode需要字节或字符串,所以将33转换为字节,然后编码:
>>> base58.b58encode(33)
Traceback (most recent call last):
...
TypeError: a bytes-like object is required (also str), not 'int'
>>> i = 33
>>> bs = i.to_bytes(1, sys.byteorder)
>>> bs
b'!'
>>> base58.b58encode(bs)
b'a'发布于 2021-11-30 02:51:46
对于Python,现在可以使用b58encode_int
>>> import base58
>>> base58.b58encode_int(33)
b'a'https://stackoverflow.com/questions/53695192
复制相似问题