int中的“无效文本”(base64.urlsafe\u b64decode(mystr))

2024-10-03 23:26:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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'
>>>

Tags: moststdinlinecall解码fileintlast
1条回答
网友
1楼 · 发布于 2024-10-03 23:26:24

使用^{}将base64解码字符串转换为int

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

相关问题 更多 >