4

I have got a Raspberry Pi setup to transmit things over socket from my project. It currently does this over cellular data on a pay as you go plan, so I would like to optimise it. All that needs to transmitted is three numbers, currently in the format of int,int,int. So, my question is which character uses the least data. I know this is nit picking, but it was more for theory.

If it were possible a list would be better as I would also need a character for negative numbers.

It will be from -100 to 100 but this could be any 200 range if reformatted at the other end

  • 2
    A range of 200 certainly fits in one byte. You might even try to sneak them along in a protocol header field that doesn't count as data on your plan. Depends really on how accounting is working there. – Helmar Jan 20 at 22:02
  • Hi @Helmar thanks for the quick reply. I am an absolute beginner to python’s socket, could you point me in a direction of embedding in the protocol header please as a quick search only revealed how to access it. Thanks – tejt Jan 22 at 6:56
3

What you want is to “pack” your 3 integers as such rather than express the in decimal as text.

If your 3 values were for instance 123, 1200 and 45678, then if you send them as three 16-bit integers it will take 6 bytes. If you send them as a human readable string made of the decimal representation, separated by commas, it would take 14 bytes.

The big difficulty with binary formats is to make sure each field has the appropriate number of bits/bytes for its range of values and precision. If any of your integers can have any value from 0 to 1 billion, you’ll need a 32-bit integer for that one. If it ranges from 0 to 100 then a single byte (8-bit integer) will be enough.

Integers can have either signed or unsigned representations. Here again, it’s a matter of matching the actual possible range of values:

  • unsigned 8 bit: 0 to 255
  • signed 8 bit: -128 to 127
  • unsigned 16 bit: 0 to 65535
  • signed 16 bit: -32768 to 32767
  • unsigned 32 bit: 0 to 4 billion something
  • signed 32 bit: -2 billion something to 2 billion something

Make sure you correctly define which format you use and that both ends use the same, and that it does indeed fit the whole range of values you need.

Of course, even if you need values from 0 to 10000, if all possible values are multiples of 100 (or of you don’t care about the last two digits), then a single byte will be enough (just divide by 100 on one end and multiply by 100 at the other end).

In some cases, using exponents may be more suitable. It really depends on the range and accuracy needed.

  • There are fancier integer types like VARINT that take a variable number of bits and will be small for small values but expand for bigger ones. They are mostly used in data streams, since the exact location of an integer in a series can only be found by handling all of the integers that came before it. – JustinB Jan 22 at 3:01
  • 1
    As @jcaron mentioned above, “packing” your 3 values will give you the smallest value possible. If you are sure your values always fit into -128 <= number <= 127, you can pack each integer into 1 byte and get the smallest value of 3 bytes total. In Python you can check the struct module, which in your case you can use like: struct.pack('bbb', 100, 23, 100) – codeadict Jan 27 at 20:27
1

Slightly too large for a comment, and does not answer your question as posted, but ...

Firstly, an upvote to @jcaron and, yes, it is a good idea to lean about data formats (integer/string/etc) and data compression (think .ZIP file).

However, an alternative would be to consider an IoT SIM card. A bunch of these have sprung up in recent years. They are data only, no voice , and beware that some might be 2G only, which your country may not support, or may drop.

The big advantage is that they charge per byte of data sent, and some don't even have a monthly fee.

I first became aware of them when buying an Onion Omega 3G expansion (*). Which recommended Hologram.

I will leave it to you to make your own decisions. Google for IoT SIM perhaps?

You can look at the Amazon listing for the Hologram SIM, which includes comparisons with others.

Here is the pricing for Hologram.

Here for ThingsMobile.

Here For Global M2M SIM.

Generally, you are talking about 5 cents for a mB, which is lot of data when you are only sending 3 integers.

  • 1
    On the 3 pay as you go I get 1p a mB on 4g – tejt Jan 23 at 16:20
  • Thanks for that. You can send a shed-load of data for £1 then ! – Mawg says reinstate Monica Jan 24 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.