M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

Stream reader.Read number of character

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.

Upvotes: 1

Views: 4265

Answers (2)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

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!
        }

Upvotes: 2

Raj Karri
Raj Karri

Reputation: 551

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.

Upvotes: 0

Related Questions