Int16 - GetBytes
Returns the specified 16-bit signed integer value as an array of bytes.
const string formatter = "{0,10}{1,13}"; public static void GetBytesInt16(short argument) { // C# Extension Method: Int16 - GetBytes byte[] byteArray = argument.GetBytes(); Console.WriteLine(formatter, argument, BitConverter.ToString(byteArray)); } public static void Main() { Console.WriteLine(formatter, "short", "byte array"); Console.WriteLine(formatter, "-----", "----------"); GetBytesInt16(0); GetBytesInt16(15); GetBytesInt16(-15); GetBytesInt16(10000); GetBytesInt16(-10000); GetBytesInt16(short.MinValue); GetBytesInt16(short.MaxValue); }
View Source
using System; public static partial class Extensions { /// <summary> /// Returns the specified 16-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 2.</returns> public static Byte[] GetBytes(this Int16 value) { return BitConverter.GetBytes(value); } }