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.
>I have a string variable.
>How can I convert the string to MemoryStream?
>
.NET 2.0
>
byte[] a = System.Text.Enc oding.GetEncodi ng("iso-8859-1").GetBytes("m y
string");
System.IO.Memor yStream m = new System.IO.Memor yStream(a);
Can I use
System.Text.Enc oding.Unicode.G etBytes(("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.Unicod e you
will always see two bytes for each character. Using
Encoding.GetEnc oding("iso-8859-1") you will always see one byte per
character, but many characters will not be represented correctly.
Comment