convert object to byte []

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

midnighthell

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top