ToBoolean Method

BitConverter.ToBoolean Method

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

Returns a Boolean value converted from one byte at a specified position in a byte array.

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

public static bool ToBoolean(
	byte[] value,
	int startIndex
)

Parameters

value
Type: System.Byte []
An array of bytes.
startIndex
Type: System.Int32
The starting position within value.

Return Value

Type: System.Boolean
true if the byte at startIndex in value is nonzero; otherwise, false.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of value minus 1.

The following code example converts elements of Byte arrays to Boolean values with the ToBoolean method.


// Example of the BitConverter.ToBoolean method.
using System;

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

   // Convert a byte array element to bool and display it.
   public static void BAToBool(System.Windows.Controls.TextBlock outputBlock, byte[] bytes, int index)
   {
      bool value = BitConverter.ToBoolean(bytes, index);

      outputBlock.Text += String.Format(formatter, index,
          BitConverter.ToString(bytes, index, 1), value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      byte[] byteArray = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };

      outputBlock.Text += String.Format(
          "This example of the BitConverter.ToBoolean( byte[ ], " +
          "int ) \nmethod generates the following output. It " +
          "converts elements \nof a byte array to bool values.\n") + "\n";
      outputBlock.Text += "initial byte array" + "\n";
      outputBlock.Text += "------------------" + "\n";
      outputBlock.Text += BitConverter.ToString(byteArray) + '\n' + "\n";
      outputBlock.Text += String.Format(formatter, "index", "array element", "bool") + "\n";
      outputBlock.Text += String.Format(formatter, "-----", "-------------", "----") + "\n";

      // Convert byte array elements to bool values.
      BAToBool(outputBlock, byteArray, 0);
      BAToBool(outputBlock, byteArray, 1);
      BAToBool(outputBlock, byteArray, 3);
      BAToBool(outputBlock, byteArray, 5);
      BAToBool(outputBlock, byteArray, 8);
      BAToBool(outputBlock, byteArray, 9);
   }
}

/*
This example of the BitConverter.ToBoolean( byte[ ], int )
method generates the following output. It converts elements
of a byte array to bool values.

initial byte array
------------------
00-01-02-04-08-10-20-40-80-FF

index   array element      bool
-----   -------------      ----
    0              00     False
    1              01      True
    3              04      True
    5              10      True
    8              80      True
    9              FF      True
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft