>> y = '-6ren">
gpt4 book ai didi

python - "invalid literal"来自 int(base64.urlsafe_b64decode(mystr))

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:30 29 4
gpt4 key购买 nike

我有一个需要解码的 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'
>>>

最佳答案

使用int.from_bytes()将 base64 解码后的字符串转换为 int

int.from_bytes(
base64.urlsafe_b64decode(
'EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='
),
'big' # the endianness
)
7390406020584230016520446236832857473226268177813448430255309703833393217993

关于python - "invalid literal"来自 int(base64.urlsafe_b64decode(mystr)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509190/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com