Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to pack different types of data into a string buffer using struct.pack_into

Tags:

python

c

struct

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?

like image 670
snap Avatar asked May 11 '11 08:05

snap


People also ask

How do you pack a string in Python?

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.

What does struct unpack return?

Strings of binary representation are converted into their original form using this function. The return of struct. unpack() is always a tuple.

How do you use structures in Python?

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.

Does Python have struct?

Python offers several data types that you can use to implement records, structs, and data transfer objects.


1 Answers

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:

  • Python docs deliberately (?) omit even a single example of use of order formatters (All examples assume a native byte order, size, and alignment with a big-endian machine.)
  • One may assume (as I and probably snap did), that "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).

like image 89
Pawel Kraszewski Avatar answered Oct 20 '22 01:10

Pawel Kraszewski