I have to get bytes array and send it into socket.
The structure looks like: 1 byte + 2 bytes + 2 bytes.
First byte is number ‘5’, second 2 bytes should be taken from variable first, third 2 bytes should be taken from variable second. What’s the right way to do this in python?
id = 5 # Fill as 1 byte
first = 42 # Fill as 2 bytes
second = 58 # The same as first
Use the
structmodule:You may want to figure out if your data is a) little or big endian and b) is signed or unsigned; the example above use big-endian ordering and signed values.
The result (in python 3) is a
bytesobject.