Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?

like image 555
Mehdi Hadeli Avatar asked Jan 19 '11 14:01

Mehdi Hadeli


People also ask

What is used to convert stream of bytes to object?

The readObject method is called by Java, when a stream of byte is deserialized to Java object. This time, output will be updated based on the current year. Conclusively serialization is the process of converting an object into stream of bytes. The converted byte stream can be used for multiple purposes.

How do you convert Bytearray?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

What is MemoryStream io?

Read Method (System.IO) Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read.


2 Answers

The easiest way to convert a byte array to a stream is using the MemoryStream class:

Stream stream = new MemoryStream(byteArray); 
like image 128
Martin Buberl Avatar answered Oct 09 '22 07:10

Martin Buberl


You're looking for the MemoryStream.Write method.

For example, the following code will write the contents of a byte[] array into a memory stream:

byte[] myByteArray = new byte[10]; MemoryStream stream = new MemoryStream(); stream.Write(myByteArray, 0, myByteArray.Length); 

Alternatively, you could create a new, non-resizable MemoryStream object based on the byte array:

byte[] myByteArray = new byte[10]; MemoryStream stream = new MemoryStream(myByteArray); 
like image 36
Cody Gray Avatar answered Oct 09 '22 07:10

Cody Gray