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


De-serialize Base64 String to Object


 / Published in: C#
 

De-serializes a Base64 String back to it's original object. First x characters should indicate the byte length of the encoded data.

  1. public static object DeserializeBase64(string s)
  2. {
  3. // We need to know the exact length of the string - Base64 can sometimes pad us by a byte or two
  4. int p = s.IndexOf(':');
  5. int length = Convert.ToInt32(s.Substring(0, p));
  6.  
  7. // Extract data from the base 64 string!
  8. byte[] memorydata = Convert.FromBase64String(s.Substring(p + 1));
  9. MemoryStream rs = new MemoryStream(memorydata, 0, length);
  10. BinaryFormatter sf = new BinaryFormatter();
  11. object o = sf.Deserialize(rs);
  12. return o;
  13. }

Report this snippet  

You need to login to post a comment.