Posted By

Anthony on 08/25/08


Tagged

String serialization base64


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

umang_nine
ddevening
khouser
ovuncgursoy


Serialize Object to a Base64 String


 / Published in: C#
 

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

  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  

You need to login to post a comment.