Write Int32 value as Big-endian using FileStream - CSharp System.IO

CSharp examples for System.IO:FileStream

Description

Write Int32 value as Big-endian using FileStream

Demo Code


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

public class Main{
        /// <summary>
        /// Write Int32 value as Big-endian
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="value"></param>
        public static void WriteInt32(FileStream stream, int value)
        {

            var byteArray = BitConverter.GetBytes(value);

            //Big endian
            stream.Write((BitConverter.IsLittleEndian) ? byteArray.Reverse().ToArray() : byteArray,
                0, byteArray.Length);
        }
}

Related Tutorials