Convert Byte array to Base64 Char Array in CSharp

Description

The following code shows how to convert Byte array to Base64 Char Array.

Example


using System;//from   www.  j  av  a  2s  .c om
using System.IO;

class Test {
    public static void Main() {

        byte[] data = { 0x12, 0x12, 0x5A, 0xFF, 0x0, 0xF0, 0x4D, 0x62, 0x78,  
            0xD2, 0xC5, 0xA1, 0x12, 0xD6, 0x0C, 0xA9, 0xA6, 0x63, 0x3D, 0xC2, 
            0xD5, 0x0F, 0xCC, 0x01, 0x12, 0x0C};

        char[] base64data =  new char[(int)(Math.Ceiling((double)data.Length / 3) * 4)];
        // The size of the char[] must 
        // be at least 4/3 the size of the source byte[] and must be 
        // divisible by 4.
        Console.WriteLine("\nByte array encoding/decoding");
        Convert.ToBase64CharArray(data, 0, data.Length, base64data, 0);
        Console.WriteLine(new String(base64data));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Development »




Console
Encoding
Environment
Random