C# string Stream 互转

时间:2023-03-08 19:53:22
C# string Stream 互转

使用C#将字符串转化成流,将流转换成字符串,代码如下:

using System.IO;
using System.Text;
namespace CSharpConvertString2Stream
{
class Program
{
static void Main( string[] args )
{
string str = "Testing 1-2-3"; //convert string 2 stream
byte[] array = Encoding.ASCII.GetBytes(str);
MemoryStream stream = new MemoryStream(array); //convert stream 2 string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
Console.WriteLine(text);
Console.ReadLine();
}
}
}