简体   繁体   English

流读取器。读取字符数

[英]Stream reader.Read number of character

Is there any Stream reader Class to read only number of char from string Or byte from byte[] ? 是否有任何流读取器类,以只读的数charstring或者bytebyte[]

forexample reading string: 例如读取字符串:

string chunk = streamReader.ReadChars(5); // Read next 5 chars

or reading bytes 或读取字节

byte[] bytes = streamReader.ReadBytes(5); // Read next 5 bytes

Note that the return type of this method or name of the class does not matter. 请注意,此方法的返回类型或类的名称无关紧要。 I just want to know if there is some thing similar to this then i can use it. 我只想知道是否有类似的东西,然后我可以使用它。

I have byte[] from midi File. 我有midi File的byte[] I want to Read this midi file in C#. 我想用C#读取此midi文件。 But i need ability to read number of bytes. 但是我需要能够读取字节数。 or chars(if i convert it to hex). 或字符(如果我将其转换为十六进制)。 To validate midi and read data from it more easily. 验证Midi并更轻松地从中读取数据。

Thanks for the comments. 感谢您的评论。 I didnt know there is an Overload for Read Methods. 我不知道读取方法有重载。 i could achieve this with FileStream. 我可以用FileStream实现。

        using (FileStream fileStream = new FileStream(path, FileMode.Open))
        {
            byte[] chunk = new byte[4];
            fileStream.Read(chunk, 0, 4);
            string hexLetters = BitConverter.ToString(chunk); // 4 Hex Letters that i need!
        }

You can achieve this by doing something like below but I am not sure this will applicable for your problem or not. 您可以通过执行以下操作来实现此目的,但是我不确定这是否适用于您的问题。

 StreamReader sr = new StreamReader(stream);
 StringBuilder S = new StringBuilder();
 while(true)
 {
 S = S.Append(sr.ReadLine());
 if (sr.EndOfStream  == true)
      {
         break;
      }
 }

Once you have value on "S", you can consider sub strings from it. 一旦对“ S”具有值,就可以考虑其中的子字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM