gpt4 book ai didi

python - 在 Python+numpy 中将字节类型转换为 uint64 数组

转载 作者:行者123 更新时间:2023-11-30 23:30:26 44 4
gpt4 key购买 nike

如果我有一个 16 字节字符串,如何将其转换为 numpy 中 2 uint64 的数组? (指定小端或大端)

最佳答案

IIUC,您可以使用np.fromstring:

>>> n = range(16)
>>> s = ''.join(map(chr, n))
>>> np.fromstring(s, dtype=np.uint64)
array([506097522914230528, 1084818905618843912], dtype=uint64)
>>> sum((256**i)*x for i,x in enumerate(n[:8]))
506097522914230528L
>>> sum((256**i)*x for i,x in enumerate(n[8:]))
1084818905618843912L

尽管您必须自己翻转字节序。尽管人们经常非常宽松地使用强制转换,但它更像是一种转换而不是强制转换。

关于python - 在 Python+numpy 中将字节类型转换为 uint64 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20585983/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com