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)

'Declaration
Public Shared Function ToChar ( _
	value As Byte(), _
	startIndex As Integer _
) As Char

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 Nothing.

ArgumentOutOfRangeException

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

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       ?


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

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

Community Additions

ADD
Show: