Two way numeric encoding

Hi,

i have numeric strings like 515123155 and i need to two way method to encode it. The main thing is that encoded string must be as short as possible, and just conversion to hex isnt an option, any ideas?

What is the maximum length of the string?

The easiest way is 'decimal', 2 digits stored in each byte?

Any special characters: + - . ,

Only numbers. From 101000000 to 931235999.

Those will fit in a 32-bit int. You can use the c library functions to convert strings to ints.

In the 8-bit arduino environment a 32-bit int is 'long int'

32-bits signed has a range up to 2 billion, unsigned up to 4 billion.

However, this won't work if your number can ever start with a 0! In this case you need to write your own mapping function.

(Research the atol function)

If you have some binary data and you want to encode it into a printable string as short as possible, the usual way as called “base 64 encoding”. In this system, each character encodes 6 bits of the data, which means that every 3 octets of data is spread over 4 printable bytes.

Your number goes from 101000000 to 931235999. This is a 29-bit range, so it will need 5 bytes. Base64 proper requires padding bytes, making your message 8 bytes long, which isn’t any saving at all, but you can ignore that if you already know how many bits you are transmitting.

If you want to encode it and dont need the result to be a prinatble string, then just pull the number apart into four bytes.

If the distribution of probable values is not even, then you can get by with even fewer bytes on average, at the cost of having to use a variable-length encoding.

For instance, lets say your values are heavily weighted towards being low numbers. You would subtract 101000000, then break the value into five 7-bit quantities. You would sent these in either order, using the top bit of the byte to indicate that there is more data.

But I suspect that what is going on here is that we are dealing with someone who doesn’t get that numbers as they are written in a chunk of code is not the same as a number in memory.