How to convert a string to MemoryStream

  • Thread starter Thread starter ad
  • Start date Start date
ad,

Attach the MemoryStream to a StreamWriter, and then call one of the
Write methods on the StreamWriter. This will convert using the Encoding set
on the StreamWriter, and convert your characters to the byte stream.

Hope this helps.
 
ad said:
I have a string variable.
How can I convert the string to MemoryStream?

..NET 2.0

byte[] a = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes("my
string");
System.IO.MemoryStream m = new System.IO.MemoryStream(a);
 
Thanks,
But what does GetEncoding("iso-8859-1") mean?
Can I use
System.Text.Encoding.Unicode.GetBytes(("my string");


Rafal M said:
ad said:
I have a string variable.
How can I convert the string to MemoryStream?

.NET 2.0

byte[] a = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes("my
string");
System.IO.MemoryStream m = new System.IO.MemoryStream(a);
 
ad said:
But what does GetEncoding("iso-8859-1") mean?

It means to use the ISO-Latin-1 encoding.
Can I use
System.Text.Encoding.Unicode.GetBytes(("my string");

Absolutely. It'll give you different results though. A stream
inherently deals with binary data, and a string is comprised of
character data. The encoding you use specifies the conversion from the
character data to binary data. For instance, using Encoding.Unicode you
will always see two bytes for each character. Using
Encoding.GetEncoding("iso-8859-1") you will always see one byte per
character, but many characters will not be represented correctly.

Jon
 
Back
Top