Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Code string to shorter meaningless string in reversible way

I'm sorry for the generic question (I don't have any past knowledge about compression and I don't know if it has a possible solution).

I have some codes of always 19 characters.

These characters can be only: A-Z, a-z, 0-9, ., :, -

An example can be something like 1995AbC...123..456Z

What I want to do is to find a way to convert in a reversible way that string to a shorter one that contains only ascii characters: something like gfSDd2H.

  • Is it possible?
  • Is there a way to do it in python?

Thanks!

like image 223
Giovanni Di Milia Avatar asked Oct 24 '25 04:10

Giovanni Di Milia


1 Answers

You can try to compress the string and the encode the result to for example base64. This of course assumes that your original strings are compressible. For strings of 19 characters this seems unlikely.

If you are allowed to persist some data you can compress the first string to 1, the second to 2, etc... and you will need to store the mapping you made in for example a database so that you can reverse it. You can then encode the number as a base 64 (or some other base) string.

This is similar to how URL shortening services work.

like image 165
Mark Byers Avatar answered Oct 27 '25 02:10

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!