Stream reader.Read number of character

让人想犯罪 __ 提交于 2021-02-11 07:26:32

问题


Is there any Stream reader Class to read only number of char from string Or byte from byte[]?

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. I want to Read this midi file in C#. 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.


回答1:


Thanks for the comments. I didnt know there is an Overload for Read Methods. i could achieve this with 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!
        }



回答2:


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.



来源:https://stackoverflow.com/questions/31173412/stream-reader-read-number-of-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!