1

So basically, I generate 16 random bytes and i then convert them to Base64. I need to transform this Base64 to an 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. The result need to be between -9223372036854775808 and 9223372036854775807.

1
  • Can you give an example of how you would use parseInt() in this code if it was written in Javascript? I'd like to know what inputs and outputs you expect Commented Apr 12, 2020 at 20:16

1 Answer 1

0

There is a builtin method to convert bytes to 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

Sign up to request clarification or add additional context in comments.

3 Comments

I think this work, it generate an Int. What is "endian" ? And why did you add a "." at this end of each function ?
My bad! somehow the periods were added at the end which shouldn't be there. Endianness determines how the bytes is stored in memory: en.wikipedia.org/wiki/Endianness
This actually don't work, well it work but not the way i want it to work... I will update my question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.