convert memory stream to byte array c# code example

Example 1: c# byte array to stream

Stream stream = new MemoryStream(byteArray);

Example 2: c# memorystream to byte array

byte[] byteArray = memoryStream.ToArray()

Example 3: c# number to byte array

public byte[] ConvertNumToByte(int Number)
{
  byte[] ByteArray = new byte[32];
  string BinString = Convert.ToString(Number, 2);
  char[] BinCharArray = BinString.ToCharArray();
  try
  {
    System.Array.Reverse(BinCharArray);
    if (BinCharArray != null && BinCharArray.Length > 0)
    {
      for (int index = 0; index < BinCharArray.Length; ++index)
      {
        ByteArray[index] = Convert.ToByte(Convert.ToString(BinCharArray[index]));
      }
    }
  }
  catch
  {
  }
  return ByteArray;
}

Example 4: convert memorystream to byte array c#

Memory stream to byte array