r/algorithms • u/mpp06 • 16d ago
Help A fixed-block encoding experiment: converting 9 decimal digits into 5 Base64 characters
I have been experimenting with a simple encoding idea that I call as '9-to-5 Encoding'.
The concept is applicable only to a sequence of integer digits, for example, digits of π without the decimal point.
Method:
Start from the leftmost digit and take every block of 9 decimal digits, interpret them as an integer, and represent that integer using just 5 characters from a 64-character alphabet.
For example:
314159265 → IkR2X
The reason I chose 9 and 5 combination is because the max value of 9 decimal digits is closest to max value of 5 Base-64 digits when converted to decimal, as compared to other combinations between decimal and Base-64. So, we waste a minimum number of empty mappings between the two.
I tested the approach on 10 million digits of π.
The interesting result:
- ZIP of original decimal text: 4,693,723 bytes
- ZIP of 9-to-5 encoded text: 4,205,187 bytes
That's approximately 10.41% less compressed storage.
It is well-known that binary representation is shortest for numbers, and this is not a claim of a new compression algorithm.
However, if it is text representation of the numbers, this '9 to 5' scheme seems to offer an advantage after zipping the text representation for 10 million digits of π. I have also found a similar advantage for 1 million digits of π.
I am thinking to explore other number datasets and compare against binary representations, especially after zipping.
I'm curious:
Has anyone come across a similar fixed-block encoding scheme? And what other experiments would you recommend?
I'd particularly appreciate feedback from people working in data compression, information theory and numerical representation.
4
u/lizardpq 16d ago
It makes sense that "pre-compress using your knowledge that the string contains only decimal digits, then do general-purpose compression" is better than just general-purpose compression.
Your observation that 109 is close to 645 =230 is related to the fact that 103 is close to 210 =1024. A binary encoding of decimal digits that uses this is https://en.wikipedia.org/wiki/Densely_packed_decimal
1
u/D-Cary 1d ago
Your approach stores 9 decimal digits (just under 29.9 bits of information) in 5 Base64 digits (exactly 30 bits of information). Very clever and efficient!
This reminds me of Ascii85, which stores fixed blocks of 4 bytes of arbitrary binary data (exactly 32 bits) into fixed blocks of 5 printable ASCII characters representing base 85 digits (just over 32.04 bites of information).
Historically several different fixed-block encoding schemes have been used to encode raw binary data over communication channels originally designed to carry only "human-readable text" -- see https://en.wikipedia.org/wiki/Binary-to-text_encoding for several examples (including Ascii85 and Base64).
Some people prefer Base32 instead of Base64 (such as https://www.crockford.com/base32.html ). For such people, perhaps your approach could be adapted to store the same 9 decimal digits into the same 5 Base64 digits, and then repacking those bits into 6 Base32 digits -- or equivalently, 3 decimal digits into 2 Base32 digits.
Some people prefer packing as much information into each byte as possible (effectively Base256). For such people, the "obvious" approach is using standard binary-coded decimal to pack 2 decimal digits into one byte (so 36 decimal digits easily fit into 18 bytes). But your approach leads to a much more efficient scheme: combined with standard Base64 encoding that re-arranges 24 bits at a time as either 3 bytes or 4 base64 digits, the combined scheme ends up packing 36 decimal digits into 20 Base64 digits and then repacking that into 15 bytes. I suspect there's a simpler way of doing that.
I am surprised that ZIP (which internally uses Huffman) treats so differently these different ways of representing "the same" decimal digits of pi. I speculate that perhaps it's an artifact of the way Huffman coding is limited to integer-code-lengths and so works great with power-of-two probabilities and not quite as well with uniformly distributed "random" decimal digits. Perhaps using Zstd or some other compressor that uses Asymmetric numeral systems (ANS) ( https://en.wikipedia.org/wiki/Asymmetric_numeral_systems ) or some other non-Huffman entropy coder might give interesting results.
0
u/Ashamed_Difficulty89 12d ago
I'm not sure, but what you mention sounds awfully similar to what's known as Word RAM model, where a text that uses only subset of a larger alphabet is encoded using a smaller alphabet to save space.