Convert a long integer into an array of bytes. - CSharp System

CSharp examples for System:Byte Array

Description

Convert a long integer into an array of bytes.

Demo Code

/// Distributed under the Mozilla Public License                            *
using System;/*w  w  w. j  a  va2  s . com*/

public class Main{
        /// <summary> Convert a long integer into an array of bytes.
        /// 
        /// </summary>
        /// <param name="value">the long to convert.
        /// </param>
        /// <param name="cnt">the number of bytes to convert.
        /// </param>
        public static byte[] toBytes(long value_Renamed, int cnt)
        {
            byte[] bytes = new byte[cnt];
            for (int i = 0; i < cnt; i++)
            {
                bytes[cnt - i - 1] = (byte) (value_Renamed & 0xff);
                value_Renamed >>= 8;
            }
            
            return bytes;
        }
}

Related Tutorials