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 array<unsigned char>^ GetBytes(
	short value
)

Parameters

value
Type: System::Int16

The number to convert.

Return Value

Type: array<System::Byte>^

An array of bytes with length 2.

The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture is little-endian or big-endian.

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

using namespace System;

void main()
{
    // Define an array of integers.
    array<Int16>^ values = { 0, 15, -15, 10000,  -10000, 
                             Int16::MinValue, Int16::MaxValue};

    // Convert each integer to a byte array.
    Console::WriteLine("{0,16}{1,10}{2,17}", "Integer", 
                       "Endian", "Byte Array");
    Console::WriteLine("{0,16}{1,10}{2,17}", "---", "------", 
                       "----------");
    for each (int value in values) {
      array<Byte>^ byteArray = BitConverter::GetBytes(value);
      Console::WriteLine("{0,16}{1,10}{2,17}", value, 
                        BitConverter::IsLittleEndian ? "Little" : " Big", 
                        BitConverter::ToString(byteArray));
    }
}
// This example displays output like the following:
//              Integer    Endian       Byte Array
//                  ---    ------       ----------
//                    0    Little            00-00
//                   15    Little            0F-00
//                  -15    Little            F1-FF
//                10000    Little            10-27
//               -10000    Little            F0-D8
//               -32768    Little            00-80
//                32767    Little            FF-7F

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: