Search code examples
javaencodingcompressiontransmission

Is there a way to compress a string into a smaller string with reversibility?


I am trying to transmit strings over the iridium network, and the costs of sending data is pretty large. I am wondering if there is a way to compress a large string, for example: {"packet":01,"reporting time":1500, "altitude":6500,"latitude":0,"longitude": 0,"ballast":34,"parachute":0}

into a much smaller string, like: f5fk43d2 . The process must be reversible, so that the data can be decoded and read on the other end. Is this possible, if so, how would I go about doing this.

I have tried this answer by j.w.r: Shortening a string in Java , however it seems irreversible. It does convert a large string into a smaller one.

The process must result in a string smaller than the original.

Any help is appreciated!


Solution

  • {"packet":01,"reporting time":1500, "altitude":6500,"latitude":0,"longitude": 0,"ballast":34,"parachute":0}

    compresses to 01,1500,65000,0,34,0 effortless.

    But you should not be sending text, you should be sending data. Using text "01" is at minimum 2 bytes. But a single byte can store up to the number 255. I would do something like
    1 (1 byte representing the packet number 0-255)
    1500 (3 bytes representing the reporting time 0-16,777,215)
    ...
    You dont need to use whole bytes, if the data will fit in less.

    Some things to consider: if you know the bounds of all the data, you dont need separators (,)
    If you have a bunch of variables you might send or not, you can include a name byte or name 4 bits(packet could be variable b0000, altitude could be variable b0011) that goes before every variable, or one that goes at the front of the message telling you which type of message you are sending.

    If you must send the variable names, as they are dynamically created. You can compress based on the allowed characters. For example, if you are only using lower case and space, like your example, that is 27 characters which will fit in under 5 bits per character. Basically, just treat the string as base 27 integer, and it compresses and decompresses easily.
    "abcd" => 1, 2, 3, 4 => 1*27^3 + 2*27^2 + 3*27^1 + 4 => 21226 or 2 bytes instead of 4