BitConverter.GetBytes Method (UInt16)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns the specified 16-bit unsigned integer value as an array of bytes.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.UInt16
The number to convert.
The following code example converts the bit patterns of UInt16 values to Byte arrays with the GetBytes method.
// Example of the BitConverter.GetBytes( ushort ) method. using System; class Example { const string formatter = "{0,10}{1,13}"; // Convert a ushort argument to a byte array and display it. public static void GetBytesUInt16(System.Windows.Controls.TextBlock outputBlock, ushort argument) { byte[] byteArray = BitConverter.GetBytes(argument); outputBlock.Text += String.Format(formatter, argument, BitConverter.ToString(byteArray)) + "\n"; } public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.Text += String.Format( "This example of the BitConverter.GetBytes( ushort ) " + "\nmethod generates the following output.\n") + "\n"; outputBlock.Text += String.Format(formatter, "ushort", "byte array") + "\n"; outputBlock.Text += String.Format(formatter, "------", "----------") + "\n"; // Convert ushort values and display the results. GetBytesUInt16(outputBlock, 15); GetBytesUInt16(outputBlock, 1023); GetBytesUInt16(outputBlock, 10000); GetBytesUInt16(outputBlock, ushort.MinValue); GetBytesUInt16(outputBlock, (ushort)short.MaxValue); GetBytesUInt16(outputBlock, ushort.MaxValue); } } /* This example of the BitConverter.GetBytes( ushort ) method generates the following output. ushort byte array ------ ---------- 15 0F-00 1023 FF-03 10000 10-27 0 00-00 32767 FF-7F 65535 FF-FF */
Show: