[英]Convert base64-encoded string into hex int
I have a base64-encoded nonce as a 24-character string: 我有一个以base64编码的随机数作为24个字符的字符串:
nonce = "azRzAi5rm1ry/l0drnz1vw=="
And I want a 16-byte int: 我想要一个16字节的int:
0x6b3473022e6b9b5af2fe5d1dae7cf5bf
My best attempt: 我最好的尝试:
from base64 import b64decode
b64decode(nonce)
>> b'k4s\x02.k\x9bZ\xf2\xfe]\x1d\xae|\xf5\xbf'
How can I get an integer from the base64 string? 如何从base64字符串中获取整数?
To get an integer from the string, you can do: 要从字符串中获取整数,可以执行以下操作:
# Python 3
decoded = int.from_bytes(b64decode(nonce), 'big')
# Python 2
decoded = int(b64decode(nonce).encode('hex'), 16)
nonce = "azRzAi5rm1ry/l0drnz1vw=="
nonce_hex = 0x6b3473022e6b9b5af2fe5d1dae7cf5bf
from base64 import b64decode
decoded = int.from_bytes(b64decode(nonce), 'big')
# PY 2
# decoded = int(b64decode(nonce).encode('hex'), 16)
assert decoded == nonce_hex
print(hex(decoded))
0x6b3473022e6b9b5af2fe5d1dae7cf5bf
You can convert like this: 您可以这样转换:
>>> import codecs
>>> decoded = base64.b64decode(nonce)
>>> b_string = codecs.encode(decoded, 'hex')
>>> b_string
b'6b3473022e6b9b5af2fe5d1dae7cf5bf'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.