Click here to Skip to main content
Click here to Skip to main content

Base32 encoding implementation in .NET

, 15 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Native .NET (C#) implementation of Base32 and zBase32 encoding.

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:

  1. Resulting string should be case insensitive.
  2. Resulting string should be as short as possible.
  3. 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:

  1. It reduces the number of potentially confusing characters.
  2. It permutes the character table so that easier character occurs more frequently.
  3. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hemant_Jangid
Team Leader
India India
I am programmer by profession and by heart. I just love it. I cant believe I am paid for programming. Smile | :)
 
Currently I have the responsibility of leading a software development team and one product. My work involves dealing with Delphi, .NET (C#) and SQL Server.
 
More on my home page

Comments and Discussions

 
GeneralAlrnative more flexible .net Base32 encoder Pinmembermhano30-Apr-10 2:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 15 Apr 2009
Article Copyright 2009 by Hemant_Jangid
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid