我有一个需要解码的base64字符串,然后将其转换为整数,以便可以“%2”它。 base64解码很容易,但是显然我对python如何实际处理二进制有一些困惑:
>>> y = 'EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck'
>>> int(base64.urlsafe_b64decode('EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='), 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 2: b'\x10V\xd2R\xad \xee\xab\xe8[g\xa1\xcaG\xd2\xbd\xe6\xff\xa5)\xae\x9f\x13\x89PEZ\xa3TV\xc1\xc9'
>>> int(base64.urlsafe_b64decode('EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='), 16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 16: b'\x10V\xd2R\xad \xee\xab\xe8[g\xa1\xcaG\xd2\xbd\xe6\xff\xa5)\xae\x9f\x13\x89PEZ\xa3TV\xc1\xc9'
>>>
答案 0 :(得分:3)
使用int.from_bytes()
将base64解码的字符串转换为int
int.from_bytes(
base64.urlsafe_b64decode(
'EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='
),
'big' # the endianness
)
7390406020584230016520446236832857473226268177813448430255309703833393217993