Read Int32 value as Big-endian from FileStream - CSharp System.IO

CSharp examples for System.IO:FileStream

Description

Read Int32 value as Big-endian from FileStream

Demo Code


using System.Linq;
using System.IO;/*from w  w  w. j av a2 s.com*/
using System.Text;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Read Int32 value as Big-endian
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static int ReadInt32(FileStream stream)
        {
            var length = sizeof(Int32);
            var buf = new byte[length];
            stream.Read(buf, 0, length);

            var value = BitConverter.ToInt32((BitConverter.IsLittleEndian) ? buf.Reverse().ToArray() : buf, 0);

            return value;
        }
}

Related Tutorials