Algorithms – Best Two-Way Compression Algorithm for 32-Bit Numbers

algorithmscompression

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?

Best Answer

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

Hexadecimal 7FFFFFFF

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.

Related Topic