GetBytes Method (Boolean)

BitConverter.GetBytes Method (Boolean)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns the specified Boolean value as an array of bytes.

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

public static byte[] GetBytes(
	bool value
)

Parameters

value
Type: System.Boolean
A Boolean value.

Return Value

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

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


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft