Export (0) Print
Expand All

BitConverter.GetBytes Method (Int16)

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

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

public static byte[] GetBytes (
	short value
)
public static byte[] GetBytes (
	short value
)
public static function GetBytes (
	value : short
) : byte[]
Not applicable.

Parameters

value

The number to convert.

Return Value

An array of bytes with length 2.

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

// Example of the BitConverter.GetBytes( short ) method.
using System;

class GetBytesInt16Demo
{
    const string formatter = "{0,10}{1,13}";
 
    // Convert a short argument to a byte array and display it.
    public static void GetBytesInt16( short 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( short ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "short", "byte array" );
        Console.WriteLine( formatter, "-----", "----------" );
          
        // Convert short values and display the results.
        GetBytesInt16( 0 );
        GetBytesInt16( 15 );
        GetBytesInt16( -15 );
        GetBytesInt16( 10000 );
        GetBytesInt16( -10000 );
        GetBytesInt16( short.MinValue );
        GetBytesInt16( short.MaxValue );
    }
}

/*
This example of the BitConverter.GetBytes( short )
method generates the following output.

     short   byte array
     -----   ----------
         0        00-00
        15        0F-00
       -15        F1-FF
     10000        10-27
    -10000        F0-D8
    -32768        00-80
     32767        FF-7F
*/

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

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

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

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

        // Convert short values and display the results.
        GetBytesInt16((short)0);
        GetBytesInt16((short)15);
        GetBytesInt16((short)-15);
        GetBytesInt16((short)10000);
        GetBytesInt16((short)-10000);
        GetBytesInt16(Int16.MinValue);
        GetBytesInt16(Int16.MaxValue);
    } //main
} //GetBytesInt16Demo

/*
This example of the BitConverter.GetBytes( short )
method generates the following output.

     short   byte array
     -----   ----------
         0        00-00
        15        0F-00
       -15        F1-FF
     10000        10-27
    -10000        F0-D8
    -32768        00-80
     32767        FF-7F
*/

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft