I'm trying to pack some unsigned int data into a string buffer created using ctypes.create_string_buffer
.
Here is the following code segment, and a running example showing the error on codepad:
import struct
import ctypes
import binascii
buf = ctypes.create_string_buffer(16)
struct.pack_into("=I=I=I", buf, 0, 1, 2, 3)
print binascii.hexlify(buf)
This yields the following error:
...
struct.error: bad char in struct format
The documentation doesn't allude to whether you can pack data of different types if the underlying buffer is of a specific C type. In this case, trying to pack unsigned int data into a string buffer with an underlying c_char type. Anyone know of a solution to do this, or is there a specific way to create a buffer that can pack any type of data?
struct.pack() struct. pack() is the function that converts a given list of values into their corresponding string representation. It requires the user to specify the format and order of the values that need to be converted.
Strings of binary representation are converted into their original form using this function. The return of struct. unpack() is always a tuple.
The module struct is used to convert the native data types of Python into string of bytes and vice versa. We don't have to install it. It's a built-in module available in Python3. The struct module is related to the C languages.
Python offers several data types that you can use to implement records, structs, and data transfer objects.
Sorry for resurrecting old topic, but I get the point of "snap" - being hit by probably similar background habit.
"the first character of the format string can be used to indicate the byte order, size and alignment of the packed data" I agree. However:
III
" consists of three format strings and we may format every one of them at will. Hence =I=I=I
. I shot myself in the foot after getting used to Ruby's array.pack, where one may freely change ordering along the expression (Ruby's equivalent is I_I_I_
in this case, as order selector comes after type).Hence I guess it might be good to add a few lines to struct.pack/unpack docs, giving examples of order & padding use (meh, padding hit me even harder... I could live with native order, but padding ruined my protocol).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With