C# – Binary reader in Big Endian?

I’m not usually one to answer my own questions, but I’ve accomplished exactly what I wanted with some simple code: class BinaryReader2 : BinaryReader { public BinaryReader2(System.IO.Stream stream) : base(stream) { } public override int ReadInt32() { var data = base.ReadBytes(4); Array.Reverse(data); return BitConverter.ToInt32(data, 0); } public Int16 ReadInt16() { var data = base.ReadBytes(2); Array.Reverse(data); … Read more

An elegant way to consume (all bytes of a) BinaryReader?

Original Answer (Read Update Below!) Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a … Read more

tech