in C# fastest way to convert a string into a MemoryStream

  • Thread starter Thread starter Daniel
  • Start date Start date
Faster then what, assembly language? Can you explain more about what you
are doing because no one can answer this message since they don't know what
you are comparing it against.
 
Peter Rilling said:
Faster then what, assembly language? Can you explain more about what you
are doing because no one can answer this message since they don't know what
you are comparing it against.

There's nothing wrong with the question - he's asking the fastest way
to do it *in C#* - he's not comparing it with any other language.
 
Daniel said:
in C# fastest way to convert a string into a MemoryStream

The simplest way is to pick a suitable encoding, use Encoding.GetBytes
to turn the string into a byte array, and create a MemoryStream
wrapping that byte array.

Assuming you really only need a Stream, however, there's a more
efficient way of doing it which is unfortunately a bit more work.
Create a StringReader from the Stream, and then (and this is the tricky
bit) write a class called ReaderStream or something similar which takes
a TextReader and an Encoding, and creates a Stream from it - you'll
need to maintain a buffer of unread bytes, and whenever you're asked to
read from the stream, either return data from that buffer or read from
the TextReader and encode the results to a byte array.

There may well already be such a class around, and it wouldn't be *too*
hard to write, but I'd stick with the first method unless efficiency is
really a big issue.
 
Peter Rilling said:
There's nothing wrong with the question - he's asking the fastest way
to do it *in C#* - he's not comparing it with any other language.
And in my opinion is there nothing wrong with asking what the OP wants to
do, because the memorystream is that fast, that it is not unthinkable that
the OP is mixing it up with a process to process message.

Cor
 
And in my opinion is there nothing wrong with asking what the OP wants to
do
Absolutely.

because the memorystream is that fast, that it is not unthinkable that
the OP is mixing it up with a process to process message.

Not sure where you're coming from on that part, I'm afraid.
 
Back
Top