FileStream fileStream = new FileStream("binaryfile.dat", FileMode.Open); BinaryReader binaryReader = new BinaryReader(fileStream); // Skip the first 4 bytes binaryReader.Skip(4); // Read the next 4 bytes as an int int intValue = binaryReader.ReadInt32(); // Close the stream binaryReader.Close(); fileStream.Close();
MemoryStream memoryStream = new MemoryStream(new byte[]{ 0x01, 0x02, 0x03, 0x04, 0x05 }); BinaryReader binaryReader = new BinaryReader(memoryStream); // Skip the first 3 bytes binaryReader.Skip(3); // Read the next 2 bytes as a short short shortValue = binaryReader.ReadInt16(); // Close the stream binaryReader.Close();In this example, we create a MemoryStream object and pass a byte array to it. We then create a BinaryReader object and pass the MemoryStream object to it. We use the Skip method to skip the first 3 bytes of the MemoryStream object and then read the next 2 bytes as a short using the ReadInt16 method. Finally, we close the BinaryReader object. Package library: System.IO.