Export (0) Print
Expand All

BitConverter.ToChar Method

Returns a Unicode character converted from two bytes at a specified position in a byte array.

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

public static char ToChar(
	byte[] value,
	int startIndex
)

Parameters

value
Type: System.Byte[]

An array.

startIndex
Type: System.Int32

The starting position within value.

Return Value

Type: System.Char
A character formed by two bytes beginning at startIndex.

ExceptionCondition
ArgumentException

startIndex equals the length of value minus 1.

ArgumentNullException

value is null.

ArgumentOutOfRangeException

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

The ToChar method converts the bytes from index startIndex to startIndex + 1 to a Char 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.

The following code example converts elements of Byte arrays to Char values (Unicode characters) with the ToChar method.

// Example of the BitConverter.ToChar method. 
using System;

class BytesToCharDemo
{
    const string formatter = "{0,5}{1,17}{2,8}";

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

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

    public static void Main( )
    {
        byte[] byteArray = {
             32,   0,   0,  42,   0,  65,   0, 125,   0, 
            197,   0, 168,   3,  41,   4, 172,  32 };

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

        // Convert byte array elements to char values.
        BAToChar( byteArray, 0 );
        BAToChar( byteArray, 1 );
        BAToChar( byteArray, 3 );
        BAToChar( byteArray, 5 );
        BAToChar( byteArray, 7 );
        BAToChar( byteArray, 9 );
        BAToChar( byteArray, 11 );
        BAToChar( byteArray, 13 );
        BAToChar( byteArray, 15 );
    }
}

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

initial byte array
------------------
20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-AC-20

index   array elements    char
-----   --------------    ----
    0            20-00
    1            00-00
    3            2A-00       *
    5            41-00       A
    7            7D-00       }
    9            C5-00       �
   11            A8-03       ?
   13            29-04       ?
   15            AC-20       ?
*/

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft