While the struct module makes handling C-like structures containing scalar values very simple, I don’t see how to sensibly handle structs which contain arrays.
For example, if I have the following C struct:
struct my_struct {
int a1[6];
double a2[3];
double d1;
double d2;
int i1;
int i2;
int a3[6];
int i3;
};
and want to unpack its values and use the same variables (a1, a2, a3, d1, d2, i1, i2, i3) in Python, I run into the problem that struct just gives me every value in a tuple individually. All information about which values are supposed to be grouped in an array is lost:
# doesn’t work!
a1, a2, d1, d2, i1, i2, a3, i3 = struct.unpack(
'6i3dddii6ii', b'abcdefghijklmnopqrstuvwxy' * 4
)
Instead, I have to slice and pull apart the tuple manually, which is a very tedious and error-prone procedure:
t = struct.unpack('6i3dddii6ii', b'abcdefghijklmnopqrstuvwxy' * 4)
a1 = t[:6]
a2 = t[6:9]
d1, d2, i1, i2 = t[9:13]
a3 = t[13:19]
i3 = t[19]
Is there any better way of handling arrays with struct?
sformat specifier (which generates a single value) and re-unpack that value as integers/shorts/whatever. But I don't know if that qualifies as a "better" way though