C# Object Binary Serialization

前端 未结 6 1712
-上瘾入骨i
-上瘾入骨i 2020-12-05 02:17

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\";

MemoryStr         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 02:58

    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...

提交回复
热议问题