Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System
BitConverter Class
GetBytes Method
 GetBytes Method (Char)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
BitConverter..::.GetBytes Method (Char)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Shared Function GetBytes ( _
    value As Char _
) As Byte()
C#
public static byte[] GetBytes(
    char value
)
Visual C++
public:
static array<unsigned char>^ GetBytes(
    wchar_t value
)
F#
static member GetBytes : 
        value:char -> byte[] 

Parameters

value
Type: System..::.Char
A character to convert.

Return Value

Type: array<System..::.Byte>[]()[]
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.

Visual Basic
' 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
C#
// 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
*/
Visual C++
// Example of the BitConverter::GetBytes( __wchar_t ) method.
using namespace System;

// Convert a __wchar_t argument to a byte array and display it.
void GetBytesChar( __wchar_t argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,10}{1,16}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( __wchar_t ) "
   "\nmethod generates the following output.\n" );
   Console::WriteLine( "{0,10}{1,16}", "__wchar_t", "byte array" );
   Console::WriteLine( "{0,10}{1,16}", "---------", "----------" );

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

/*
This example of the BitConverter::GetBytes( __wchar_t )
method generates the following output.

 __wchar_t      byte array
 ---------      ----------
                     00-00
                     20-00
         *           2A-00
         3           33-00
         A           41-00
         [           5B-00
         a           61-00
         {           7B-00
*/

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker