Base64 Serializer : Base64 « Development Class « C# / C Sharp






Base64 Serializer

 
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class Base64Serializer
{
    public static string Serialize(object value)
    {
        MemoryStream ws = new MemoryStream();
        BinaryFormatter sf = new BinaryFormatter();
        sf.Serialize(ws, value);
        byte[] bytes = ws.GetBuffer();
        return bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
    }
    public static object Deserialize(string base64)
    {
        int p = base64.IndexOf(':');
        int length = Convert.ToInt32(base64.Substring(0, p));
        byte[] data = Convert.FromBase64String(base64.Substring(p + 1));
        MemoryStream rs = new MemoryStream(data, 0, length);
        BinaryFormatter sf = new BinaryFormatter();
        return sf.Deserialize(rs);
    }
}

   
  








Related examples in the same category

1.Double To Int 64 Bits, Int 64 Bits To Double
2.Base 64 encode
3.Base64 Encoder
4.Url Encode Base 64
5.The Base64 utility class performs base64 encoding and decoding.
6.The Base64 utility class performs base64 encoding and decoding. The resulting binary data is returned as an array of bytes.
7.Decodes all or part of the input base64 encoded StringBuffer
8.Encode To 64
9.Encrypt Key and Decrypt Key with Convert.ToBase64String and Convert.FromBase64String
10.Serializes and deserializes an entity to a base 64 string
11.Base64 from http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java
12.Convert To Base 64 String
13.Get Xml value as Integer 64
14.Create a hex string from an Int64 value