问题
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.
回答1:
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 couples of chars from the source alphabet with
a single char from the target alphabet).
回答2:
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.
来源:https://stackoverflow.com/questions/27274926/how-to-compress-alphanumeric-strings