字符串與數據流之間的轉換


可以把字符串轉換為MemoryStream。也可以把MenoryStream轉換為字符串。

 

下面Insus.NET寫了幾個方法:

 

 class Bq
    {
        public string Input { get; set; }

        public byte[] Byte { get; set; }

        public MemoryStream MemStream { get; set; }

        public byte[] GetByte()
        {
            return Encoding.ASCII.GetBytes(Input);
        }

        public MemoryStream GetMemoryStream()
        {
            byte[] byteArray = Byte;
            return new MemoryStream(byteArray);
        }

        public string GetString()
        {
            StreamReader reader = new StreamReader(MemStream);
            return reader.ReadToEnd();
        }
    }
Source Code

 

控制台測試以上的方法:

 

class Program
    {
        static void Main(string[] args)
        {
            Bq objBq = new Bq();
            objBq.Input = "Hello Insus.NET";

            byte[] Bytes = objBq.GetByte();




            objBq.Byte = Bytes;
            MemoryStream ms = objBq.GetMemoryStream();





            objBq.MemStream = ms;
            string output = objBq.GetString();


            Console.WriteLine(output);
        }
    }
Source Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM