Convert a string to stream

  • Thread starter Thread starter Randy
  • Start date Start date
Randy said:
Is there an easy way to convert a string to a stream?

Does System.IO.StringReader does what you want? If not, you could use
an Encoding to get the bytes from the string, then
System.IO.MemoryStream to build a stream from those bytes.
 
I found this...
Stream stream = new
MemoryStream(System.Text.ASCIIEncoding.GetBytes(printbuffer));
but when I compile, it gives me this...
An object reference is required for the nonstatic field, method, or property
'System.Text.Encoding.GetBytes(string)'

What does this mean?
Thanks for any help...
 
Hi Randy?

What you mean with that?

The best I can think of would be like

MemoryStream mstream = new MemoryStream( BitConverter.GetBytes(
string_var) );

Cheers,
 
Hi Randy,

You are missing the ASCII property form ASCIIEncoding:
System.Text.ASCIIEncoding.ASCII.GetBytes("test");
instead of:
System.Text.ASCIIEncoding.GetBytes(printbuffer)


BTW, I posted before an incorrect post, BitConverter.GetBytes() does not
have an overload for string , please ignore it.

Cheers,
 
Oh yeah...thanks for the info. I see now...


Ignacio Machin ( .NET/ C# MVP ) said:
Hi Randy,

You are missing the ASCII property form ASCIIEncoding:
System.Text.ASCIIEncoding.ASCII.GetBytes("test");
instead of:
System.Text.ASCIIEncoding.GetBytes(printbuffer)


BTW, I posted before an incorrect post, BitConverter.GetBytes() does not
have an overload for string , please ignore it.

Cheers,
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
You are missing the ASCII property form ASCIIEncoding:

Note that it's actually a property of Encoding, not ASCIIEncoding -
it's just visible through ASCIIEncoding as well, of course. I
personally prefer to read:

Encoding ascii = Encoding.ASCII;

rather than

Encoding ascii = ASCIIEncoding.ASCII;

- the latter gives the impression that the property is a member of the
ASCIIEncoding class.

(Obviously the same goes for the other built-in encodings.)
 
Back
Top