I am trying to construct a module for python in C. The module has several lines and methods, but one of the most important is to pass, efficiently, the integer representation of the concatenation of a uint32_t array to python. For example, from code below
{
uint32_t state[3] = {0x00000300, 0x00001100, 0x00022200}
char charstate[12];
memcpy(charstate, state, 12);
return Py_BuildValue("s#", charstate, 12);
}
I expect in python the integer representation of the concatenation of that hex elements that is 0x000003000000110000022200 = 14167099467300633453056. As you can see I tried to use 's#', but without success. Could you give me an advice, please?
I would just do an array, as you have in C.
unsigned longis a type wide enough to storeuint32_t.If the value has more then 64-bits, there is no "correct" type. Is so, move the calculations to python side.
If you want a single value of
14167099467300633453056on python side, then the widest available type in C can't fit it -unsigned long longhas (at least) 64-bits, while the value is expected to have 96 bits. I suggest either to:and then convert the resulting value to number on python side, which would be analogous to
int("3000000110000022200", 16)in python.After
memcpyon a little endian system the content ofcharstatewould be:which on call to
Py_BuildValue("s#", charstateto a python string, that would look like:then you would have to convert each byte by byte to an integer and shift the result on each byte by 8 (or multiply by 2^8) to some accumulator to get the resulting value.