Home About

December 10th, 2008

Encoding and Decoding Binary data using Base64 with C# - 1

It is often necessary to convert binary data to and from strings. One common way of encoding such information is through Base-64 encoding. The following post shows you how to convert a byte array to a Base64 string , and back again.

What is Base64?

Base-64 encoded information uses the most common characters: A-Z,a-z and 0-9, allowing for save storage of binary date in places that do not support binary.

The most common situation is e-mail, but it is also often used to encode binary data in XML files or other text file formats.

An example of Base-64 encoded data:

CRc6tbZ+170rwAQ5bPNYXvc8M7cx+xeJiyrFBpda

Implementing it in C#

The following example shows how to convert a byte array to a base-64 encoded string and back again. All the actual work is done by the System.Convert class. In the example below we use System.Security.Cryptography to fill our test array with random data.

using System;
using System.Security.Cryptography;

namespace Base64
{
    class MainClass
    {
        public static bool VerifyEqual(byte[] one, byte[] two)
        {
            if (one.Length != two.Length) return false;
            for (int Lp = 0; Lp < one.Length; Lp++)
                if (!one[Lp].Equals(two[Lp])) return false;
            return true;
        }

        public static void Main(string[] args)
        {
            byte[] DataToEncode = new byte[30];

            // Fill the byte array with random data
            RandomNumberGenerator Generator = RandomNumberGenerator.Create();
            Generator.GetNonZeroBytes(DataToEncode);

            // Convert the byte array to a base-64 string
            string Base64String = Convert.ToBase64String(DataToEncode);
            Console.WriteLine("{0}",Base64String);

            // Convert the base-64 string back to a byte array
            byte[] DecodedData = Convert.FromBase64String(Base64String);

            // Verify that the data was decoded correctly
            if (!VerifyEqual(DecodedData,DataToEncode))
                Console.WriteLine("Byte[]'s do not match!");
            else
                Console.WriteLine("Byte[]'s match!");

        }
    }
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Reddit

Tags: ,

One Response to “Encoding and Decoding Binary data using Base64 with C#”

  1. denglish Says:

    Thank you for the code, this helped me complete and important project. I referenced you in my blog documentation.

    Best regards, denglish

Leave a Reply


Recent Comments
  • Hakbor: This works fine. But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient...
  • Alberto: Your plugin is very useful; I installed it on several different blogs I manage and I’m very happy with...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to...
  • berties: any english speaking playing on a taiwanese server?