// Convert a byte array to an Object public static object ToObject(byte[] bytes) { switch (bytes[0]) { case 0: return null; default: case 1: using (var m = new MemoryStream()) { m.Write(bytes, 1, bytes.Length-1); m.Flush(); m.Seek(0, SeekOrigin.Begin); using (var zs = new System.IO.Compression.DeflateStream(m, System.IO.Compression.CompressionMode.Decompress, true)) { var f = new BinaryFormatter(); return f.Deserialize(zs); } } break; case 2: using (var m = new MemoryStream()) { m.Write(bytes, 1, bytes.Length-1); m.Flush(); m.Seek(0, SeekOrigin.Begin); using (var zs = new System.IO.Compression.DeflateStream(m, System.IO.Compression.CompressionMode.Decompress, true)) { return System.Text.Encoding.UTF8.GetString(zs.ToArray()); } } case 3: return System.Text.Encoding.UTF8.GetString(bytes.Skip(1).ToArray()); case 4: return ""; } }