serialization with normal txt file?

  • Thread starter Thread starter MP
  • Start date Start date
M

MP

How I can use serialization and get "normal" txt file as output?
I am using BinaryFormater but I need "normal" file on output.

Thanks
 
MP said:
How I can use serialization and get "normal" txt file as output?
I am using BinaryFormater but I need "normal" file on output.

Well, it's binary, it's not text, so you can't get "normal" text
from it.

You can encode it into various text formats like BinHex or
Base64.

Convert.ToBase64String(byte[]) is useful.

BinHex is better because you can see the hex of the bytes,
but it's more difficult because you have to loop through
the byte array and format each byte (not that hard,
just a little more code than Base64).

Have your BinaryFormatter Serialize to a MemoryStream,
then get the buffer from the MemoryStream and format
it however you like.

MemoryStream ms = new MemoryStream(4096);

BinaryFormatter frm = new BinaryFormatter();
frm.Serialize(ms, yourObjectInstance);

// note this may have to be ms.Position - 1,
// but I forget off the top of my head
byte[] payload = new byte[ms.Position];

ms.Position = 0;
ms.Write(payload, 0, payload.Length);
ms.Close()

Console.WriteLine( Convert.ToBase64String(payload) );

// - or -

for( int i = 0; i < payload.Length; i++ )
{
Console.Write( payload.ToString("X2") + " ");
}
Console.WriteLine();

-c
 
Back
Top