I have an use case where I need to encode a 38 digit number into a string of length 18 to 20.
I'm using a ByteBuffer
with capacity of 16 bytes in java to store two long values. Then I try to encode it to a string without any special characters. I tried base32 encoding which resulted in string of length 26, but I need a much smaller string for my use case.
Is there any workaround for this?
By special characters, I mean excluding anything that is not in the A-Z or 0-9 range.
No, I'm fairly certain it can't be done. Remember that characters are binary data. They are just interpreted as characters when printed for human consumption. Due to their nature, binary numbers are usually "packed" as efficiently as possible. Encoding in something like a Base64
character set (which coincidentally is identical to your requirements of a-zA-Z0-9
plus \+
and =
for padding), increases the size of the string relative to the original information. Due to packing every 6 bits of source converted to an 8 bit character you increase the values original size by 33%
.
Another way to look at it is to create a table of 16 bit (0-65535) values. And for every 16 bit grouping, substitute a different character. But you would need 65536
unique characters to do this to restore the data. So you aren't gaining anything.
Perhaps some compression algorithm like a huffman code might work. But that and other compression algorithms tend to work best on textual data and worse on binary data (compiled programs, audio, etc);