Introduction
This article presents a library which implements Base32 encoding and zBase32 encoding using C#.
Background
I was working on a project where I needed to send some binary information to my application and that information was to be processed by humans. I had the following considerations:
- Resulting string should be case insensitive.
- Resulting string should be as short as possible.
- Resulting string should not contain potentially confusing characters such as o, 0, i, l, 1 etc.
I had the choice of Hex encoding, Base64 encoding, and Base32 encoding. Base64 encoding was most efficient as far as length of output is concerned, but since it produces case sensitive encoded strings, I had to let it go.
Hex encoding produces encoded strings which are too long (double the size of the original data) so this option was also down. Base32 encoding seemed good enough, but Wikipedia told me that there is a modified version of Base32 encoding called zBase32 encoding which has the following advantages over plain Base32 encoding:
- It reduces the number of potentially confusing characters.
- It permutes the character table so that easier character occurs more frequently.
- It uses an intelligent padding scheme (or rather it does not use padding) which results in shorter encoded text in some cases.
I decided to use zBase32 encoding, but I couldn't find any standard implementation on the .NET platform so I ended up writing the implementation myself.
Using the code
This library is pretty simple by any standards. It has only two classes:
Base32Encoder
: Standard Base32 encoding implementation.ZBase32Encoder
: Derived from Base32Encoder
. Uses the base class for the modified zBase32 encoding implementation.
Using any of the above encoder is identical. It can be done like this:
public void Sample () {
var zBase32 = new ZBase32Encoder ();
byte[] inputData = new byte[] { 0xF0, 0xBF, 0xC7 };
string encodedText = zBase32.Encode (inputData);
byte[] outputData = zBase32.Decode (encodedText);
Console.WriteLine ("Original: {0}, Encoded: {1}, Decoded: {2}",
ToString (inputData), encodedText, ToString (outputData));
}
The output of the above code is:
Original: F0BFC7, Encoded: 6n9hq, Decoded: F0BFC7
Points of interest
The thing to note here is the difference between Base32 encoding and zBase32 encoding when it comes to the length of the output text. Here is the summary:
Input length | Base32 encoded text length | zBase32 encoded text length |
1 | 8 | 2 |
2 | 8 | 4 |
3 | 8 | 5 |
4 | 8 | 7 |
5 | 8 | 8 |
6 | 16 | 10 |
7 | 16 | 12 |
8 | 16 | 13 |
9 | 16 | 15 |
When the input data length is not natural (multiple of 5), Base32 encoding takes way too many characters to encode the same text.
History
- First revision of the article.