Home > Back-end >  binary encoding Little endian in python
binary encoding Little endian in python

Time:09-01

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.

import secrets
b = secrets.token_bytes(16)
val = int.from_bytes(b[:8], "little")
  • Related