How do I convert struct System.Byte
byte[]
to a System.IO.Stream
object in C#?
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.
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
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.
The easiest way to convert a byte array to a stream is using the MemoryStream
class:
Stream stream = new MemoryStream(byteArray);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With