Export (0) Print
Expand All

BitConverter.GetBytes Method (Char)

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

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

'Declaration
Public Shared Function GetBytes ( _
	value As Char _
) As Byte()
'Usage
Dim value As Char
Dim returnValue As Byte()

returnValue = BitConverter.GetBytes(value)
public static byte[] GetBytes (
	char value
)
public static function GetBytes (
	value : char
) : byte[]
Not applicable.

Parameters

value

A character to convert.

Return Value

An array of bytes with length 2.

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

// Example of the BitConverter.GetBytes( char ) method.
import System.*;

class GetBytesCharDemo
{
    private static String formatter = "{0,10}{1,16}";

    // Convert a char argument to a byte array and display it.
    public static void GetBytesChar(char argument)
    {
        ubyte byteArray[] = BitConverter.GetBytes(argument);
        Console.WriteLine(formatter, (Char)argument, 
            BitConverter.ToString(byteArray));
    } //GetBytesChar

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the BitConverter.GetBytes( char ) " 
            + "\nmethod generates the following output.\r\n"));
        Console.WriteLine(formatter, "char", "byte array");
        Console.WriteLine(formatter, "----", "----------");

        // Convert char values and display the results.
        GetBytesChar('\0');
        GetBytesChar(' ');
        GetBytesChar('*');
        GetBytesChar('3');
        GetBytesChar('A');
        GetBytesChar('[');
        GetBytesChar('a');
        GetBytesChar('{');
    } //main
} //GetBytesCharDemo

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

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft