BinaryReader.ReadInt32は、データがリトルエンディアン形式であることを想定しています。提示したデータはビッグエンディアンです。
BinaryWriterがInt32をメモリに書き込む方法の出力を示すサンプルプログラムを次に示します。
namespace Endian {
using System;
using System.IO;
static class Program {
static void Main() {
int a = 2051;
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)) {
writer.Write(a);
}
foreach (byte b in stream.ToArray()) {
Console.Write("{0:X2} ", b);
}
}
Console.WriteLine();
}
}
}
これを実行すると、次の出力が生成されます。
03 08 00 00
2つの間で変換するには、を使用して4バイトを読み取り、配列を逆にBinaryReader.ReadBytes(4)
してから、を使用して使用可能なintに変換します。BitConverter.ToInt32
byte[] data = reader.ReadBytes(4);
Array.Reverse(data);
int result = BitConverter.ToInt32(data);