Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress alphanumeric strings?

Tags:

java

io

I want to shrink Strings like -1234B56789C;ABC1D3E/FGH4IJKL which are approx 20 - 25 case-insensitive chars.

My goal is to have an alphanumeric string that is a maximum of 16 characters. They must remain human readable.

Is that possible? Are there algorithms that can be used to compress alphanumeric string that also has some special chars?

It must also be possible to revert the compression.

like image 565
membersound Avatar asked Sep 30 '22 07:09

membersound


2 Answers

I think in general it's not possible unless you use a different target alphabet.
As far as I understand currently your source alphabet is 0-9 and A-Z.
If you extend your target alphabet to include also certain N>0 other chars,
then you can encode an input string with less characters that it originally had
(because e.g. you can encode pairs of chars from the source alphabet with
single chars from the target alphabet).

like image 131
peter.petrov Avatar answered Oct 24 '22 13:10

peter.petrov


You could attempt an LZW-like approach and look for common patterns in your input. For example - if you find that "1234" occurs often in your strings then you could encode that as "Q".

This approach cannot consistently achieve your requirements of a 16-character encoded string unless you can prove that the compression mappings you choose will always occur in the source with sufficient regularity to achieve a 16-character length.

like image 2
OldCurmudgeon Avatar answered Oct 24 '22 14:10

OldCurmudgeon