BitConverter.ToUInt16 Method

Definition

Overloads

ToUInt16(ReadOnlySpan<Byte>)

Converts a read-only byte-span into a 16-bit unsigned integer.

ToUInt16(Byte[], Int32)

Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.

ToUInt16(ReadOnlySpan<Byte>)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Important

This API is not CLS-compliant.

Converts a read-only byte-span into a 16-bit unsigned integer.

C#
[System.CLSCompliant(false)]
public static ushort ToUInt16(ReadOnlySpan<byte> value);

Parameters

value
ReadOnlySpan<Byte>

A read-only span containing the bytes to convert.

Returns

An 16-bit unsigned integer representing the converted bytes.

Attributes

Exceptions

The length of value is less than 2.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

ToUInt16(Byte[], Int32)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Important

This API is not CLS-compliant.

Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.

C#
[System.CLSCompliant(false)]
public static ushort ToUInt16(byte[] value, int startIndex);

Parameters

value
Byte[]

The array of bytes that includes the two bytes to convert.

startIndex
Int32

The starting position within value.

Returns

A 16-bit unsigned integer formed by two bytes beginning at startIndex.

Attributes

Exceptions

startIndex equals the length of value minus 1.

value is null.

startIndex is less than zero or greater than the length of value minus 1.

Examples

The following code example converts elements of Byte arrays to UInt16 values with the ToUInt16 method.

C#
// Example of the BitConverter.ToUInt16 method.
using System;

class BytesToUInt16Demo
{
    const string formatter = "{0,5}{1,17}{2,10}";

    // Convert two byte array elements to a ushort and display it.
    public static void BAToUInt16( byte[ ] bytes, int index )
    {
        ushort value = BitConverter.ToUInt16( bytes, index );

        Console.WriteLine( formatter, index,
            BitConverter.ToString( bytes, index, 2 ), value );
    }

    public static void Main( )
    {
        byte[] byteArray = {
            15, 0, 0, 255, 3, 16, 39, 255, 255, 127 };

        Console.WriteLine(
            "This example of the BitConverter.ToUInt16( byte[ ], " +
            "int ) \nmethod generates the following output. It " +
            "converts elements \nof a byte array to ushort values.\n" );
        Console.WriteLine( "initial byte array" );
        Console.WriteLine( "------------------" );
        Console.WriteLine( BitConverter.ToString( byteArray ) );
        Console.WriteLine( );
        Console.WriteLine( formatter, "index", "array elements",
            "ushort" );
        Console.WriteLine( formatter, "-----", "--------------",
            "------" );

        // Convert byte array elements to ushort values.
        BAToUInt16( byteArray, 1 );
        BAToUInt16( byteArray, 0 );
        BAToUInt16( byteArray, 3 );
        BAToUInt16( byteArray, 5 );
        BAToUInt16( byteArray, 8 );
        BAToUInt16( byteArray, 7 );
    }
}

/*
This example of the BitConverter.ToUInt16( byte[ ], int )
method generates the following output. It converts elements
of a byte array to ushort values.

initial byte array
------------------
0F-00-00-FF-03-10-27-FF-FF-7F

index   array elements    ushort
-----   --------------    ------
    1            00-00         0
    0            0F-00        15
    3            FF-03      1023
    5            10-27     10000
    8            FF-7F     32767
    7            FF-FF     65535
*/

Remarks

The ToUInt16 method converts the bytes from index startIndex to startIndex + 1 to a UInt16 value. The order of bytes in the array must reflect the endianness of the computer system's architecture. For more information, see the Remarks section of the BitConverter class topic.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0