Export (0) Print
Expand All

BitConverter.GetBytes Method (UInt16)

Returns the specified 16-bit unsigned integer value as an array of bytes.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[CLSCompliantAttribute(false)]
public static byte[] GetBytes(
	ushort value
)

Parameters

value
Type: System.UInt16
The number to convert.

Return Value

Type: System.Byte[]
An array of bytes with length 2.

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
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft