Share via


Converting a byte array to a memorystream

Question

Tuesday, September 18, 2007 4:53 PM

I have the following C# code that writes data to a string from a byte array (called 'buffer') so that it can be sent to the console.  I need to convert that byte array to a memorystream.  Here is the piece with the lead-up code:

 

byte[] buffer = new byte[blobSize];

IntPtr source = Marshal.AllocHGlobal(blobSize);

FPStream streamRef = new FPStream(source, blobSize, FPStream.StreamDirection.OutputFromCentera);

t.BlobRead(streamRef);

Marshal.Copy(source, buffer, 0, blobSize);

Marshal.FreeHGlobal(source);

//Console.WriteLine(converter.GetString(buffer));

streamRef.Close();

 

The REMed out line is the line that normally gets sent to the console, which I need to go to a memorystream.  How do I convert the buffer into a memorystream?

 

Scott

All replies (2)

Tuesday, September 18, 2007 5:05 PM ✅Answered

one of the memorystream constructor takes byte[] as input

 

Code Snippet

MemoryStream ms = new MemoryStream(buffer);

 

 

 

 


Tuesday, September 18, 2007 5:25 PM

Thank you ... that worked.

 

Now I have to return the memorystream back to the calling app (this code is in an assembly).  Here is what I have but I think the return line is wrong. 

 

namespace RetrieveContent

{

    public class GetContent

    {

        [STAThread]

        public memorystream Main(string[] args)

        {

            try

                    MemoryStream ms = new MemoryStream(buffer);

                    ms return

….

 

Can you tell me how close I am?  btw:  what is the "[STAThread]" do?  (I am a VB programmer trying to work with some C# code).

 

Scott