Convert a long integer into an array of bytes, little endian format. - CSharp System

CSharp examples for System:Byte Array

Description

Convert a long integer into an array of bytes, little endian format.

Demo Code

/// Distributed under the Mozilla Public License                            *
using System;/*from ww  w . j a va 2  s .  com*/

public class Main{
        /// <summary> Convert a long integer into an array of bytes, little endian format. 
        /// (i.e. this does the same thing as toBytes() but returns an array 
        /// in reverse order from the array returned in toBytes().
        /// </summary>
        /// <param name="value">the long to convert.
        /// </param>
        /// <param name="cnt">the number of bytes to convert.
        /// </param>
        public static byte[] toBytesLittleEndian(long value_Renamed, int cnt)
        {
            byte[] bytes = new byte[cnt];
            for (int i = 0; i < cnt; i++)
            {
                bytes[i] = (byte) (value_Renamed & 0xff);
                value_Renamed >>= 8;
            }
            
            return bytes;
        }
}

Related Tutorials