binary encoding Little endian in python

huangapple go评论88阅读模式
英文:

binary encoding Little endian in python

问题

我通常使用golang进行实现,现在我需要实现一个Python项目。我有以下指令(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])

我需要编写这些指令的Python版本,但是我找不到任何方法来从Uint64创建一个小端字节序的数组。是否有任何可用的解决方案可以帮助我!

英文:

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!

答案1

得分: 0

等效的Python程序如下所示。

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

The equivalent Python program would be as follows.

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

huangapple
  • 本文由 发表于 2022年9月1日 17:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73566855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定