简体   繁体   English

将 Base64 编码字节转换为 Int

[英]Convert Base64 encoded bytes to Int

So basically, I generate 16 random bytes and i then convert them to Base64.所以基本上,我生成 16 个随机字节,然后将它们转换为 Base64。 I need to transform this Base64 to an Int.我需要将此 Base64 转换为 Int。 I've searched all over the internet, i found out how to convert to hex, and many other but none seem to work.我在互联网上搜索过,我发现了如何转换为十六进制,还有很多其他的,但似乎都没有。 This is the code I use to generate the nonce:这是我用来生成随机数的代码:

import base64
nonce = base64.encodebytes(os.urandom(16))

I need a function a bit like the parseInt() in JavaScript.我需要一个 function,有点像 JavaScript 中的 parseInt()。 The result need to be between -9223372036854775808 and 9223372036854775807.结果需要介于 -9223372036854775808 和 9223372036854775807 之间。

There is a builtin method to convert bytes to int:有一个内置方法可以将字节转换为 int:

int.from_bytes(nonce, "big")  # big endian
int.from_bytes(nonce, "little")  # little endian

Python docs: https://docs.python.org/3/library/stdtypes.html#int.from_bytes Python 文档: https://docs.python.org/3/library/stdtypes.html#int.from_bytes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM