BitConverter.GetBytes Method (Char)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns the specified Unicode character value as an array of bytes.

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

Public Shared Function GetBytes (
	value As Char
) As Byte()

Parameters

value
Type: System.Char

A character to convert.

Return Value

Type: System.Byte()

An array of bytes with length 2.

The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture is little-endian or big-endian.

The following code example converts the bit patterns of Char values (Unicode characters) to Byte arrays with the GetBytes method.

' Example of the BitConverter.GetBytes( Char ) method.
Imports System
Imports Microsoft.VisualBasic

Module GetBytesCharDemo

    Const formatter As String = "{0,10}{1,16}"

    ' Convert a Char argument to a Byte array and display it.
    Sub GetBytesChar( argument As Char )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 

    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( Char ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Char", "Byte array" )
        Console.WriteLine( formatter, "----", "----------" )

        ' Convert Char values and display the results.
        GetBytesChar( Chr( 0 ) )
        GetBytesChar( " "c )
        GetBytesChar( "*"c )
        GetBytesChar( "3"c )
        GetBytesChar( "A"c )
        GetBytesChar( "["c )
        GetBytesChar( "a"c )
        GetBytesChar( "{"c )
    End Sub 
End Module

' This example of the BitConverter.GetBytes( Char )
' method generates the following output.
' 
'       Char      Byte array
'       ----      ----------
'                      00-00
'                      20-00
'          *           2A-00
'          3           33-00
'          A           41-00
'          [           5B-00
'          a           61-00
'          {           7B-00

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: