Export (0) Print
Expand All

BitConverter.GetBytes Method (UInt16)

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

This method is not CLS-compliant.  

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

[CLSCompliantAttribute(false)] 
public static byte[] GetBytes (
	ushort value
)
/** @attribute CLSCompliantAttribute(false) */ 
public static byte[] GetBytes (
	UInt16 value
)
CLSCompliantAttribute(false) 
public static function GetBytes (
	value : ushort
) : byte[]

Parameters

value

The number to convert.

Return Value

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 GetBytesUInt16Demo
{
    const string formatter = "{0,10}{1,13}";
 
    // Convert a ushort argument to a byte array and display it.
    public static void GetBytesUInt16( ushort argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument, 
            BitConverter.ToString( byteArray ) );
    }
       
    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the BitConverter.GetBytes( ushort ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "ushort", "byte array" );
        Console.WriteLine( formatter, "------", "----------" );
          
        // Convert ushort values and display the results.
        GetBytesUInt16( 15 );
        GetBytesUInt16( 1023 );
        GetBytesUInt16( 10000 );
        GetBytesUInt16( ushort.MinValue );
        GetBytesUInt16( (ushort)short.MaxValue );
        GetBytesUInt16( 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
*/

// Example of the BitConverter.GetBytes( ushort ) method.
import System.*;

class GetBytesUInt16Demo
{
    private static String formatter = "{0,10}{1,13}";

    // Convert a ushort argument to a byte array and display it.
    public static void GetBytesUInt16(UInt16 argument)
    {
        ubyte byteArray[] = BitConverter.GetBytes(argument);
        Console.WriteLine(formatter, (UInt16)argument,
            BitConverter.ToString(byteArray));
    } //GetBytesUInt16

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the BitConverter.GetBytes( ushort )"
            + " \nmethod generates the following output.\n"));
        Console.WriteLine(formatter, "ushort", "byte array");
        Console.WriteLine(formatter, "------", "----------");

        // Convert ushort values and display the results.
        GetBytesUInt16((UInt16)15);
        GetBytesUInt16((UInt16)1023);
        GetBytesUInt16((UInt16)10000);
        GetBytesUInt16(UInt16.MinValue);
        GetBytesUInt16((UInt16)Int16.MaxValue);
        GetBytesUInt16(UInt16.MaxValue);
    } //main
} //GetBytesUInt16Demo

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft