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)

public static byte[] GetBytes (
	char 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.
using System;

class GetBytesCharDemo
{
    const string formatter = "{0,10}{1,16}";
 
    // Convert a char argument to a byte array and display it.
    public static void GetBytesChar( char argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument, 
            BitConverter.ToString( byteArray ) );
    }
       
    public static void Main( )
    {
        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( '{' );
    }
}

/*
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:
© 2014 Microsoft