Compartir a través de


BitConverter.ToChar Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function ToChar ( _
    value As Byte(), _
    startIndex As Integer _
) As Char
public static char ToChar(
    byte[] value,
    int startIndex
)

Parameters

  • startIndex
    Type: System.Int32
    The starting position within value.

Return Value

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

Exceptions

Exception Condition
ArgumentException

startIndex equals the length of value minus 1.

ArgumentNullException

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

ArgumentOutOfRangeException

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 Char values (Unicode characters) with the ToChar method.

' Example of the BitConverter.ToChar method.

Module Example

   Const formatter As String = "{0,5}{1,17}{2,8}"

   ' Convert two Byte array elements to a Char and display it.
   Sub BAToChar(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal bytes() As Byte, ByVal index As Integer)

      Dim value As Char = BitConverter.ToChar(bytes, index)

      outputBlock.Text &= String.Format(formatter, index, _
          BitConverter.ToString(bytes, index, 2), value) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim byteArray As Byte() = { _
           32, 0, 0, 42, 0, 65, 0, 125, 0, 197, _
            0, 168, 3, 41, 4, 172, 32}

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.ToChar( Byte( ), " & _
          "Integer ) " & vbCrLf & "method generates the " & _
          "following output. It converts elements " & vbCrLf & _
          "of a Byte array to Char values." & vbCrLf) & vbCrLf
      outputBlock.Text &= "initial Byte array" & vbCrLf
      outputBlock.Text &= "------------------" & vbCrLf
      outputBlock.Text &= BitConverter.ToString(byteArray) & vbCrLf
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(formatter, "index", "array elements", "Char") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "-----", "--------------", "----") & vbCrLf

      ' Convert Byte array elements to Char values.
      BAToChar(outputBlock, byteArray, 0)
      BAToChar(outputBlock, byteArray, 1)
      BAToChar(outputBlock, byteArray, 3)
      BAToChar(outputBlock, byteArray, 5)
      BAToChar(outputBlock, byteArray, 7)
      BAToChar(outputBlock, byteArray, 9)
      BAToChar(outputBlock, byteArray, 11)
      BAToChar(outputBlock, byteArray, 13)
      BAToChar(outputBlock, byteArray, 15)
   End Sub
End Module

' This example of the BitConverter.ToChar( Byte( ), Integer )
' 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.
using System;

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

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

      outputBlock.Text += String.Format(formatter, index,
          BitConverter.ToString(bytes, index, 2), value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      byte[] byteArray = {
             32,   0,   0,  42,   0,  65,   0, 125,   0, 
            197,   0, 168,   3,  41,   4, 172,  32 };

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

      // Convert byte array elements to char values.
      BAToChar(outputBlock, byteArray, 0);
      BAToChar(outputBlock, byteArray, 1);
      BAToChar(outputBlock, byteArray, 3);
      BAToChar(outputBlock, byteArray, 5);
      BAToChar(outputBlock, byteArray, 7);
      BAToChar(outputBlock, byteArray, 9);
      BAToChar(outputBlock, byteArray, 11);
      BAToChar(outputBlock, byteArray, 13);
      BAToChar(outputBlock, 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       ?
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.