zoukankan      html  css  js  c++  java
  • 字符串与数据流之间的转换

    可以把字符串转换为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
  • 相关阅读:
    ssh 远程命令
    POJ 2287
    POJ 2376
    hihoCoder1488
    POJ1854
    HDU 5510
    HDU 4352
    CodeForces 55D
    HDU 1517
    CodeForces 1200F
  • 原文地址:https://www.cnblogs.com/insus/p/8138008.html
Copyright © 2011-2022 走看看