Partager via


BitConverter.GetBytes Method (Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the specified Boolean value as an array of bytes.

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

Syntax

'Declaration
Public Shared Function GetBytes ( _
    value As Boolean _
) As Byte()
public static byte[] GetBytes(
    bool value
)

Parameters

Return Value

Type: array<System.Byte[]
An array of bytes with length 1.

Examples

The following code example converts the bit patterns of Boolean values to Byte arrays with the GetBytes method.

' Example of the BitConverter.GetBytes( Boolean ) method.

Module Example

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

   ' Convert a Boolean argument to a Byte array and display it.
   Sub GetBytesBool(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Boolean)

      Dim byteArray As Byte() = BitConverter.GetBytes(argument)
      outputBlock.Text &= String.Format(formatter, argument, _
          BitConverter.ToString(byteArray)) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.GetBytes( " & _
          "Boolean ) " & vbCrLf & "method generates the " & _
          "following output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Boolean", "Byte array") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "-------", "----------") & vbCrLf

      ' Convert Boolean values and display the results.
      GetBytesBool(outputBlock, False)
      GetBytesBool(outputBlock, True)
   End Sub
End Module

' This example of the BitConverter.GetBytes( Boolean )
' method generates the following output.
'
'    Boolean      Byte array
'    -------      ----------
'      False              00
'       True              01
// Example of the BitConverter.GetBytes( bool ) method.
using System;

class Example
{
   const string formatter = "{0,10}{1,16}";

   // Convert a bool argument to a byte array and display it.
   public static void GetBytesBool(System.Windows.Controls.TextBlock outputBlock, bool argument)
   {
      byte[] byteArray = BitConverter.GetBytes(argument);
      outputBlock.Text += String.Format(formatter, argument,
          BitConverter.ToString(byteArray)) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format(
          "This example of the BitConverter.GetBytes( bool ) " +
          "\nmethod generates the following output.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "bool", "byte array") + "\n";
      outputBlock.Text += String.Format(formatter, "----", "----------") + "\n";

      // Convert bool values and display the results.
      GetBytesBool(outputBlock, false);
      GetBytesBool(outputBlock, true);
   }
}

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

      bool      byte array
      ----      ----------
     False              00
      True              01
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.