Base64 Encode Binary Data

We can use ToBase64CharArray and FromBase64CharArray from System.Convert class to convert binary data to and from a Base64-encoded char array.

We can use the ToBase64String and FromBase64String of the Convert class for characters.


using System;
using System.IO;
using System.Text;

class MainClass
{
    public static byte[] DecimalToByteArray(decimal src)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write(src);
                return stream.ToArray();
            }
        }
    }

    public static decimal ByteArrayToDecimal(byte[] src)
    {
        using (MemoryStream stream = new MemoryStream(src))
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                return reader.ReadDecimal();
            }
        }
    }
    public static string StringToBase64(string src)
    {
        byte[] b = Encoding.Unicode.GetBytes(src);
        return Convert.ToBase64String(b);
    }
    public static string Base64ToString(string src)
    {
        byte[] b = Convert.FromBase64String(src);
        return Encoding.Unicode.GetString(b);
    }
    public static string DecimalToBase64(decimal src)
    {
        byte[] b = DecimalToByteArray(src);
        return Convert.ToBase64String(b);
    }
    public static decimal Base64ToDecimal(string src)
    {
        byte[] b = Convert.FromBase64String(src);
        return ByteArrayToDecimal(b);
    }
    public static string IntToBase64(int src)
    {
        byte[] b = BitConverter.GetBytes(src);
        return Convert.ToBase64String(b);
    }
    public static int Base64ToInt(string src)
    {
        byte[] b = Convert.FromBase64String(src);
        return BitConverter.ToInt32(b, 0);
    }

    public static void Main()
    {
        byte[] data = { 0x04, 0x43, 0x5F, 0xFF, 0x0, 0xF0, 0x2D, 0x62, 0x78, 
                         0x22, 0x15, 0x51, 0x5A, 0xD6, 0x0C, 0x59, 0x36, 0x63, 0xBD, 0xC2, 
                        0xD5, 0x0F, 0x8C, 0xF5, 0xCA, 0x0C};

        char[] base64data =
            new char[(int)(Math.Ceiling((double)data.Length / 3) * 4)];

        Console.WriteLine("Byte array encoding/decoding");
        Convert.ToBase64CharArray(data, 0, data.Length, base64data, 0);
        Console.WriteLine(new String(base64data));
        Console.WriteLine(BitConverter.ToString(
            Convert.FromBase64CharArray(base64data, 0, base64data.Length)));

        // Encode and decode a string. 
        Console.WriteLine(StringToBase64("this is a test"));
        Console.WriteLine(Base64ToString("dABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0AA=="));

        // Encode and decode a decimal. 
        Console.WriteLine(DecimalToBase64(2.5m));
        Console.WriteLine(Base64ToDecimal("KDjBUP07BoEPAAAAAAAJAA=="));

        // Encode and decode an int. 
        Console.WriteLine(IntToBase64(35789));
        Console.WriteLine(Base64ToInt("zYsAAA=="));

    }
}

The output:


Byte array encoding/decoding
BENf/wDwLWJ4IhVRWtYMWTZjvcLVD4z1ygw=
04-43-5F-FF-00-F0-2D-62-78-22-15-51-5A-D6-0C-59-36-63-BD-C2-D5-0F-8C-F5-CA-0C
dABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0AA==
this is a test
KDjBUP07BoEPAAAAAAAJAA==
285998345545.563846696
zYsAAA==
35789
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.