I usually use golang for implementations and I need to implement a python project. I have the following instructions (Golang)
import "encoding/binary"
arBytes := make([]byte, 16)
_, err := rand.Read(arBytes)
if err != nil {
return false, err
}
a0 = binary.LittleEndian.Uint64(arBytes[0:8])
I need to write the python's version for these instructions but i do not found any way to create an array of little Endian from Uint64. Is there any available solution that can help!
CodePudding user response:
The equivalent Python program would be as follows.
- Use
secrets.token_bytes(16)
to generate 16 random bytes, likerand.Read(arBytes)
would. - Then use
int.from_bytes(..., "little")
on that byte array to interpret the bytes as a little-endian integer.from_bytes
defaults to unsigned values.
import secrets
b = secrets.token_bytes(16)
val = int.from_bytes(b[:8], "little")