# Best two-way compression algorithm for 32-bit numbers

I need to compress an id for marketing campaigns. The current campaign id is 32-bit integer but obviously this is too long for a customer to type by hand. I would like to compress this to minimum length while still maintaining two-way compression/decompression and uniqueness of id.

What algorithm should I use?

A 32-bit id is so little data with so little redundancy to exploit that a true "compression algorithm" is likely not going to help much. On the other hand, simply using a higher numeric base where you use letters as well as digits to represent the number probably accomplishes exactly what you're after. For example, here's the largest possible 32-bit integer value in different bases:

``````Binary      1111111111111111111111111111111

Ternary     12112122212110202101

Quaternary  1333333333333333

Quinary     13344223434042

Senary      553032005531

Octal       17777777777

Decimal     2147483647

Duodecimal  4BB2308A7

Vigesimal   1DB1F927

Base 36     ZIK0ZJ
``````

Base 36 is a logical stopping point because the letters A-Z and the digits 0-9 give you exactly 36 symbols, so going to even higher bases would require introducing something less obvious and possibly difficult to type.

A six-digit "number" should be short enough for anyone to easily type by hand. I'll assume you can work out the trivial algorithm to convert a number from one base to another on your own.

• Good stuff. BTW, base 36 is commonly called: Hexatrigesimal Mar 23, 2016 at 11:10
• base 63 and 85 are also fairly common
– jk.
Mar 23, 2016 at 16:24