convert object to byte []

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

I need to get byte values from an object into a byte[] in order to write the
bytes to file.

An object (vSound) is passed to a function.
From the Autos window I can see that the Value of vSound is {System.Array}
and the Type is {System.Object}.
vSound has no properties or methods that allow you to get access to the byte
values.
When I type
?vSound
in the command window, I get:
{System.Array}
[1]: 36
[2]: 0
[3]: 8
[4]: 0
[5]: 220
....
[800]: 5
I can't figure out how to legally cast the object to a byte array.
I tried
byte[] mybyte;
mybyte = (Byte[]) vSound;
Any ideas? Thanks.
 
Use BinaryFormatter class in (check MSDN)
using System.Runtime.Serialization.Formatters.Binary;

it returns Serialized object into the Stream;

So you can use this Code:

Stream
myStream=File.Open("myfile.bin",FileMode.Create,FileAccess.ReadWrite);

BinaryFormatter formatter=new BinarryFomatter();

formatter.Serialize(myStream, someObject);



And your object will be automatic put into file as Stream. And later if you
want to deserialize, justa call method for deserialize
formatter.Deserialize(myStream)
 
Back
Top