MemoryStream memStream = new MemoryStream(); int value = 42; memStream.WriteValueS32(value); byte[] bytes = memStream.ToArray();
using (var memStream = new MemoryStream()) using (var writer = new BinaryWriter(memStream)) { int value = 1234; writer.Write(value); byte[] bytes = memStream.ToArray(); }In this example, we use a BinaryWriter to write a 32-bit integer value to a MemoryStream. We then convert the contents of the stream to a byte array using ToArray. Both MemoryStream and BinaryWriter are part of the System.IO namespace in C#. Package/library: System.IO.