除了以下方法,还有直接的方法吗?
np.uint32(int.from_bytes(b'\xa3\x8eq\xb5', 'big'))
np.fromstring
已经不再推荐使用了,现在请使用np.frombuffer
。您也可以传递一个正常的numpy数据类型:
import numpy as np
np.frombuffer(b'\xa3\x8eq\xb5', dtype=np.uint32)
关键是获取正确的数据类型。要从字符串中读取大端 uint32,数据类型(作为字符串)是 '>u4'
。
>>> np.fromstring(b'\xa3\x8eq\xb5', dtype='>u4')
array([2744021429], dtype=uint32)
int.from_bytes
技巧是无法实现的)。我不确定数据类型。
np.fromstring(b'\xa3\x8eq\xb5', dtype='<i')