When you binary serialize an object in .Net using the BinaryFormatter you end up with a byte array which is obviously meaningless to humans.
Does this byte array correspond to a more meaningful string representation which is human readable? Or do you need to fully deserialize it in order to make it more human readable?
I would expect that the binary formatter has some intermediate string representation of the object which it uses before emitting the byte array. That would be perfect for my needs...
I tried Base64 encoding the byte array, but I just ended up with gibberish.
EDIT:
As explained in my answer, UTF8 encoding is the best you can get.
The reason I want to do this is so that I can diff two binarySerializations and only store the first serialization and the diff, and was interested in seeing how the serialization worked in order to work out how best to diff the byte array.
How to analyse contents of binary serialization stream? discusses the format of binary-serialization in greater detail, and also has a link to an analyzer of sorts.
Theres no fully human readable intermediate representation, but using Console.WriteLine(System.Text.Encoding.UTF8.GetString(bytes));
will return something that might be workable depending on the exact purposes for which it's needed.
Note that only some bytes can be decoded using UTF8, as only parts of the byte array are UTF8 encoded. There'll be plenty of unfound-symbold in the resulting string.
As an example serialising the following and converting the result to a UTF8 string:
namespace MyNamespace
{
[Serializable]
public class Class
{
private readonly int _int = 42;
public string String { get; } = "MyString";
}
}
results in:
" ???? ConsoleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null MyNamespace.Class _int<String>k__BackingField * MyString"
Which isn't completely useless...