stringencryptionintegercompression

How to compress an integer to a smaller string of text?


Given a random integer, for example, 19357982357627685397198. How can I compress these numbers into a string of text that has fewer characters?

The string of text must only contain numbers or alphabetical characters, both uppercase and lowercase.

I've tried Base64, but couldn't get it to work in the way I want.

Each number represents 10 different combinations. While in the alphabet, letters can represent 26 different combinations. This makes it so that you can store more data in 5 alphabetical letters, than in a 5 digit integer. So it is possible to store more data in a string of characters than in an integer in mathematical means, but I just can't figure it out.


Solution

  • You switch from base 10 to eg. base 62 by repeatedly dividing by 62 and record the remainders from each step like this:

    Converting 6846532136 to base62: 
    
       Operation         Result   Remainder
    6846532136 / 62    110427937     42
     110427937 / 62      1781095     47
       1781095 / 62        28727     21
         28727 / 62          463     21
           463 / 62            7     29
             7 / 62            0      7
    

    Then you use the remainder as index in to a base62 alphabet of your choice eg:

    0         1         2         3         4         5         6
    01234567890123456789012345678901234567890123456789012345678901
    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
    
    Giving:  H (7) d (29) V (21) V (21) v (47) q (42)   = HdVVvq
                                                          ------