I have string object. I need to pass this data to another object of type XYZ. But this object of type XYZ is taking only System.IO.Stream. So how to convert the string data into a stream so that object of XYZ type can use this string data?
You'll have to pick a text encoding to use to translate the string into a byte array, then use a MemoryStream
to call your function. For example:
using(System.IO.MemoryStream ms = new System.IO.MemoryStream(
System.Text.Encoding.UTF16.GetBytes(yourString)))
{
XYZ(ms);
}
You can alter UTF16
to be whatever encoding you'd like to use to pass the string.
Assuming you want the string's stream encoded in UTF8:
System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes( "the string"));
Depending on what you really want to do, you might be better served using the StringReader class. It's not an IO.Stream, but it makes for easy text-oriented reading of a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With