Home About

December 10th, 2008

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

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!");
           
        }
    }
}
Be Sociable, Share!

Tags: ,

2 Responses 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

  2. Nish Says:

    Wonderful!!!
    Thanks for your help……


Most popular

    Sorry. No data so far.

Recent Comments
  • Juan Romero: Hi there, it’s a neat little class, but I believe you could do the same thing with the WebClient...
  • anthosh: Hey, THank you very much for your tutorial. It was awesome. But i have a problem that i am not able to...
  • bian: how to get passphase if i have encrypt and decrypt string?? Thanks alot
  • Michael: Hi, I really like your post, thanks a lot, it really helped clear up a few things I could not remember how...
  • Bharat Prajapati: i was trying to import keyword dictionary to this plugin which is in csv format, but i get an error...