Compressing your data
You should consider compressing your data to make maximum use of every byte in your payload.
Consider an application that needs to send 3 measurement readings, each reading is a decimal from 0 to 100, with 2 decimal places.
A typical CSV representation of this might look like this:
If transmitted as CSV, in text mode, that data would consume 17 bytes.
Consider the accuracy required
Do we really need 2 decimal places? This sounds obvious, but a really simple way to reduce the size of your data is to only transmit what you need. Let's consider that we can live with only 1 d.p:
That's reduced the payload to 14 bytes.
Now let's convert our measurements to integers by multiplying by 10:
(that's saved another 3 bytes, and we're still in text mode - and we could go further by removing the delimiters).
Each measurement now requires an integer range of 0-1000. 1000 in hex is 0x3e8, so we need a 10-bit field to store each measurement.
The table below shows how you could bit-pack these 10-bit fields into 8-bit bytes.
Using bitwise operators:
Measurement A = (byte << 2) + (byte >> 6)
Measurement B = ( (byte & 0x3F) << 4 ) + ( byte >> 4 )
Measurement C = ( (byte & 0x0F) << 6 ) + ( byte >> 2 )
The same data is now consuming less than 4 bytes!
Updated about 5 years ago