Partager via


BitConverter.GetBytes Method (UInt16)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function GetBytes ( _
    value As UShort _
) As Byte()
[CLSCompliantAttribute(false)]
public static byte[] GetBytes(
    ushort value
)

Parameters

Return Value

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

Examples

The following code example converts the bit patterns of UInt16 values to Byte arrays with the GetBytes method.

' Example of the BitConverter.GetBytes( UInt16 ) method.

Module Example

   Const formatter As String = "{0,10}{1,13}"

   ' Convert a UInt16 argument to a Byte array and display it.
   Sub GetBytesUInt16(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As UInt16)

      Dim byteArray As Byte() = BitConverter.GetBytes(argument)
      outputBlock.Text &= String.Format(formatter, argument, _
          BitConverter.ToString(byteArray)) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.GetBytes( UInt16 ) " & _
          vbCrLf & "method generates the following " & _
          "output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "UInt16", "Byte array") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "------", "----------") & vbCrLf

      ' Convert UInt16 values and display the results.
      GetBytesUInt16(outputBlock, Convert.ToUInt16(15))
      GetBytesUInt16(outputBlock, Convert.ToUInt16(1023))
      GetBytesUInt16(outputBlock, Convert.ToUInt16(10000))
      GetBytesUInt16(outputBlock, Convert.ToUInt16(0))
      GetBytesUInt16(outputBlock, Convert.ToUInt16(Int16.MaxValue))
      GetBytesUInt16(outputBlock, Convert.ToUInt16(65535))
   End Sub
End Module

' This example of the BitConverter.GetBytes( UInt16 )
' method generates the following output.
' 
'     UInt16   Byte array
'     ------   ----------
'         15        0F-00
'       1023        FF-03
'      10000        10-27
'          0        00-00
'      32767        FF-7F
'      65535        FF-FF
// 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
*/

Version Information

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

Platforms

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