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
)
public static char ToChar (
	byte[] value, 
	int startIndex
)
public static function ToChar (
	value : byte[], 
	startIndex : int
) : char

Parameters

value

An array.

startIndex

The starting position within value.

Return Value

A character formed by two bytes beginning at startIndex.

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

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

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

// Example of the BitConverter.ToChar method.
import System.*;

class BytesToCharDemo
{
    private static String formatter = "{0,5}{1,17}{2,8}";

    // Convert two byte array elements to a char and display it.
    public static void BAToChar(ubyte bytes[], int index)
    {
        char value = BitConverter.ToChar(bytes, index);
        Console.WriteLine(formatter, (Int32)index,
            BitConverter.ToString(bytes, index, 2), (Char)value);
    } //BAToChar

    public static void main(String[] args)
    {
        ubyte 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 ) \n"
            + "method 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);
    } //main
} //BytesToCharDemo

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

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
Show: