Int32 - GetBytes

Returns the specified 32-bit signed integer value as an array of bytes.

Try it

const string formatter = "{0,10}{1,13}";

public static void GetBytesInt16(Int32 argument)
{
	// C# Extension Method: Int32 - GetBytes
    byte[] byteArray = argument.GetBytes();
    Console.WriteLine(formatter, argument, BitConverter.ToString(byteArray));
}

public static void Main()
{
    Console.WriteLine(formatter, "Int32", "byte array");
    Console.WriteLine(formatter, "-----", "----------");

    GetBytesInt16(0);
    GetBytesInt16(15);
    GetBytesInt16(-15);
    GetBytesInt16(10000);
    GetBytesInt16(-10000);
    GetBytesInt16(Int32.MinValue);
    GetBytesInt16(Int32.MaxValue);
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     Returns the specified 32-bit signed integer value as an array of bytes.
    /// </summary>
    /// <param name="value">The number to convert.</param>
    /// <returns>An array of bytes with length 4.</returns>
    public static Byte[] GetBytes(this Int32 value)
    {
        return BitConverter.GetBytes(value);
    }
}