Click here to Skip to main content
13,559,672 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 15 Apr 2009
Licenced CPOL

Base32 encoding implementation in .NET

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


This article presents a library which implements Base32 encoding and zBase32 encoding using C#.


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 lengthBase32 encoded text lengthzBase32 encoded text length

When the input data length is not natural (multiple of 5), Base32 encoding takes way too many characters to encode the same text.


  • First revision of the article.


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralMy vote is excellent Pin
goracio8-Feb-11 1:45
membergoracio8-Feb-11 1:45 
GeneralNo good if new to .NET Pin
stayko3-Jan-11 18:40
memberstayko3-Jan-11 18:40 
GeneralAlrnative more flexible .net Base32 encoder Pin
mhano30-Apr-10 2:00
membermhano30-Apr-10 2:00 
GeneralMy vote of 2 Pin
Phillip4659-Nov-09 5:38
memberPhillip4659-Nov-09 5:38 
General[My vote of 1] Show and explain the code Pin
PIEBALDconsult15-Apr-09 9:53
memberPIEBALDconsult15-Apr-09 9:53 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180527.1 | Last Updated 15 Apr 2009
Article Copyright 2009 by Hemant_Jangid
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid