I have a byte stream with 3 bytes that I have to cast into an unsigned int 32. In C I would just use bzero to zero out the memory region of my integer and then copy the memory from the stream, something like
char* stream = ...;
Uint32 myInt;
bzero(myInt,4);
memcopy(myInt, stream[0], stream[3])
Question:
How would I do this in Python?
Cheers
Even in C you should not use
memcpyfor binary de-serialization, because it doesn’t deal with binary representation, i.e. endianess, sign and the like. Explicitly decode binary values from a fixed representation in the stream to the platform’s native representation.In Python, you’d read the bytes from the stream, pad them according to the endianess, and then unpack them with
struct:fileobjis a file-like object, representing the opened stream, i.e. a file opened withopen(filename, 'rb').