-1

I have a byte array consist of 32 bytes. I want to read 4 bytes from index position 16 to 19. How can i point binary reader to start reading from index 16. I am trying these commands

byte[] trace ; // 32 byte array
using (FileStream s = File.OpenRead(filename))
using (BinaryReader r = new BinaryReader(s))
 {
   r.baseStream.Seek(position,SeekOrigin.Begin);
   byte[] by = r.ReadBytes(4);
 }

but i don't know what to put at position?

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
omar000
  • 3
  • 3
  • no. i do not want to start reading at 16th index of file. i want to read at 16th index of array which i have already extracted from file – omar000 Aug 02 '14 at 14:10
  • 1
    But you're reading only 4 bytes from the file in array `by`. There are not 16 elements in the array, but 4, indexed 0 up to 3. – Marcel N. Aug 02 '14 at 14:11
  • I think your example is wrong. You have the byte array `trace` of length 32 and you need to read 4 bytes starting with byte 16. Right? – Marcel N. Aug 02 '14 at 14:13
  • "*i want to read at 16th index of array which i have already extracted from file*". Please really make your question really, really clear: Is your problem about reading data from the BinaryStream, or is your problem that you do not know how to work with/how to access array elements? –  Aug 02 '14 at 14:15
  • i am talking about byte[] trace. it contain 32 elements. and now i want to read 4 elements from 16 to 19 – omar000 Aug 02 '14 at 14:16
  • `var b1 = trace[16]` `var b2 = trace[17]` `var b3 = trace[18]` `var b4 = trace[19]`... I think you really, really need to spend more time reading tutorials about the basics of C#... –  Aug 02 '14 at 14:17
  • i am sorry . i could not explain it completely. my problem is to return particular index of array instead of its value. – omar000 Aug 02 '14 at 14:18
  • Sorry, you are very unclear... what stops you from returning an array index, what is your problem? If you are unable to describe your problem, nobody can help you, unfortunately... –  Aug 02 '14 at 14:19
  • Alright, I'm out. Let me know if my answer doesn't help you and I'll delete it. – Marcel N. Aug 02 '14 at 14:20
  • thanks all of you. Marcel N answer is what i was really looking for. i apologize for inconvenience. i am not very much expert in c#. therefore my question looks stupid to all of you. – omar000 Aug 02 '14 at 14:38

1 Answers1

0

I think I got it (although your sample in the question is not very clear).

You have the byte array trace with 32 elements in it and you want to read 4 bytes starting with position 16.

Assuming that endianness is not a variable, you can use this to read the 4 bytes as an int value or byte array:

using(var memStream = new MemoryStream(trace)) 
{
  //position the stream
  using(var reader = new BinaryReader(memStream)
  {
    memStream.Seek(16, SeekOrigin.Begin);
    var intValue = reader.ReadInt32();

    memStream.Seek(16, SeekOrigin.Begin);
    //now read a byte array
    var byteArray = reader.ReadBytes(4);
  }
}
Marcel N.
  • 13,726
  • 5
  • 47
  • 72