Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Object Binary Serialization

I want to make a binary serialize of an object and the result to save it in a database.

Person person = new Person(); person.Name = "something";  MemoryStream memorystream = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(memorystream, person); 

How can I transform memorystream in a string type to be saved in database, and after this to be able to deserialize the object?

like image 666
Emanuel Avatar asked Nov 17 '09 13:11

Emanuel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


1 Answers

What you're really asking for is a safe way of representing arbitrary binary data as text and then converting it back again. The fact that it stores a serialized object is irrelevant.

The answer is almost to use Base 64 (e.g. Convert.ToBase64String and Convert.FromBase64String). Do not use Encoding.UTF8.GetString or anything similar - your binary data is not encoded text data, and shouldn't be treated as such.

However, does your database not have a data type for binary data? Check for BLOB, IMAGE and BINARY types...

like image 137
Jon Skeet Avatar answered Sep 23 '22 16:09

Jon Skeet