Packing byte fields and an array object into struct

Ned Batchelder ned at nedbatchelder.com
Wed Nov 13 15:47:25 EST 2013


On Wednesday, November 13, 2013 3:41:03 PM UTC-5, krishna... at gmail.com wrote:
> Thanks for your reply Ned!
> 
> I tried this your suggestion and this is what it complains...
> 
> os_inst_bytes = struct.pack('7BI512s', 0, 0x51, 0x10, 5, 0, 0xD, 0x80, 0, '')
> 
> ---------------------------------------------------------------------------
> error                                     
> Traceback (most recent call last)
> <ipython-input-6-d36f45a8d3e6> in <module>()
> ----> 1 os_inst_bytes = struct.pack('7BI512s', 0, 0x51, 0x10, 5, 0, 0xD, 0x80, 0, "")
> 
> error: argument for 's' must be a bytes object
> 

OK, looks like you are using Python 3 (it helps to specify these things up-front).  Use b"" to get an empty byte-string.


> In [7]: 
> 
> 
> And about the bytearray() call, I want to pass a mutable object to the IOCTL to be able to get the data back from the driver. Without bytearray(), the ioctl with mutable flag set to 1 would complain.

OK, keep the bytearray call if it helps.

--Ned.



More information about the Python-list mailing list