Serialize Object to a Base64 String


/ Published in: C#
Save to your folder(s)

Serializes an object to a Base64 string. The first few characters of the serialized string indicate the byte length of the encoded data.


Copy this code and paste it in your HTML
  1. public static string SerializeBase64(object o)
  2. {
  3. // Serialize to a base 64 string
  4. byte[] bytes;
  5. long length = 0;
  6. MemoryStream ws = new MemoryStream();
  7. BinaryFormatter sf = new BinaryFormatter();
  8. sf.Serialize(ws, o);
  9. length = ws.Length;
  10. bytes = ws.GetBuffer();
  11. string encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
  12. return encodedData;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.