Convert a big endian byte array to little endian - CSharp System

CSharp examples for System:Byte Array

Description

Convert a big endian byte array to little endian

Demo Code


using System.Text;
using System.Linq;
using System;/*from  w w  w .ja  v  a 2s .  c  o  m*/

public class Main{
        /// <summary>
        /// Convert a big endian byte array to little endian
        /// </summary>
        /// <param name="value">
        /// The byte array to convert
        /// </param>
        /// <returns>
        /// The converted byte array
        /// </returns>
        private static byte[] ConvertBigToLittleEndian(byte[] value)
        {
            /*
             * We don't want to do this with LINQ (value.Reverse().ToArray())
             * because it's way too slow (~10x) and allocates a lot of memory
             */
            var result = new byte[value.Length];

            for (var i = 0; i < value.Length; i++)
            {
                result[i] = value[value.Length - 1 - i];
            }

            return result;
        }
}

Related Tutorials