Say you have b'\x04' and b'\x00' how can you combine them as b'\x0400'?
5 Answers
In my application I am receiving a stream of bytes from a sensor. I need to combine two of the bytes to create the integer value.
Hossein's answer is the correct solution.
The solution is the same as when one needs to bit shift binary numbers to combine them for instance if we have two words which make a byte, high word 0010 and low word 0100. We can't just add them together but if we bit shift the high word to the left four spaces we can then or the bits together to create 00100100. By bit shifting the high word we have essencially multiplied it by 16 or 10000.
In hex example above we need to shift the high byte over two digits which in hex 0x100 is equal to 256. Therefore, we can multiple the high byte by 256 and add the low byte.
Comments
It has a simple solution like this: 0x0400 = 0x04 × 256 + 0x00