BitConverter::GetBytes Method (Char)

 

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

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

public:
static array<unsigned char>^ GetBytes(
	wchar_t value
)

Parameters

value
Type: System::Char

A character to convert.

Return Value

Type: array<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( __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
*/

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: