Search code examples
c#binarymemorystream

Read Int32 from binary at specific position


I have a memorystream reading a specific part of my data. From the binary I want one ReadInt32 value from position 5-8. How do I achieve this in:

using (var reader = new BinaryReader(stream))
{

  somebyte1
  somebyte2
  somebyte3

  //get only this value
  int v = reader.ReadInt32;

}

Solution

  • Move the base stream to the position you want to read from:

    stream.Seek(4, SeekOrigin.Begin);
    
    using (var reader = new BinaryReader(stream))
    {
        int v = reader.ReadInt32;
    }