User blog comment:Hseiken/Saving massive amounts of data in petitcom/@comment-5334617-20140801220957

Using mathematics and modulo arithmetic, you can sometimes make systems that 'compress' information even better than converting to and from strings of decimal numbers. Imagine the following scenario:

ITEM 1 = 0 - 2 ITEM 2 = 0 - 2 ITEM 3 = 0 - 1 ITEM 4 = 0 - 3 ITEM 5 = 0 - 2

Putting these into decimal strings means a minimum of 5 digits, and a minimum of 2 characters in the range 0-255.

Instead, make a list of the number of possibilities for each 'item': 3, 3, 2, 4, 3. Multiply all these together, you get 216, which is not greater than 256, so you can put all these into ONE character.

To 'bundle' the five values into one byte, use the expression. The multiplying numbers come from the list above, minus the last one: 3, 3, 2, 4.

To 'unbundle', you use the same list. First, divide by all the numbers in the list before the item you want to retrieve, and round down, with  (for the first item, skip this step). Then, use, the modulo operator, with the number in the list for the item you want. For instance, for ITEM4, the numbers in the list before it are 3, 3, 2, and its number is 4. To get its value, say. The complete unbundling would be:

Compression can be even more effective if you use not just a single character, but multiple characters, as your basic unit for storing numerical values.