简体   繁体   中英

How to write a uint32 integer into a 4 bytes sequence using numpy?

I have declared a numpy ndarray containing a single unsigned integer:

import numpy as np  
foo=np.array([3600000],dtype='uint32')

I would like to store this array into a 4 bytes sequence. I have already tested a few options, such as the struct.pack() and struct.pack_into() methods, to no avail. I have also tried the numpy.ndarray.tobytes() method, which converts the content of my array into a bytes object:

bar=np.ndarray.tobytes(foo)

Alas, the ouput byte object has a length of 4 bits! How can I control the number of bits of my output bytes sequence?

Many thanks for your help!

As pointed out by Warren Weckesser, the length of a bytes object is given in bytes, not in bits. So in this case, bar indeed has a length of 4 bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM