次の方法で共有


BitConverter.ToBoolean Method

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

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

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

Syntax

'Declaration
Public Shared Function ToBoolean ( _
    value As Byte(), _
    startIndex As Integer _
) As Boolean
public static bool ToBoolean(
    byte[] value,
    int startIndex
)

Parameters

  • 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.

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

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

Examples

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

' Example of the BitConverter.ToBoolean method.

Module Example

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

   ' Convert a Byte array element to Boolean and display it.
   Sub BAToBool(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal bytes() As Byte, ByVal index As Integer)

      Dim value As Boolean = BitConverter.ToBoolean(bytes, index)

      outputBlock.Text &= String.Format(formatter, index, _
          BitConverter.ToString(bytes, index, 1), value) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim byteArray As Byte() = _
          {0, 1, 2, 4, 8, 16, 32, 64, 128, 255}

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.ToBoolean( Byte( ), " & _
          "Integer ) " & vbCrLf & "method generates the " & _
          "following output. It converts elements of " & vbCrLf & _
          "a Byte array to Boolean values." & vbCrLf) & vbCrLf
      outputBlock.Text &= "initial Byte array" & vbCrLf
      outputBlock.Text &= "------------------" & vbCrLf
      outputBlock.Text &= BitConverter.ToString(byteArray) & vbCrLf
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(formatter, "index", "array element", "Boolean") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "-----", "-------------", "-------") & vbCrLf

      ' Convert Byte array elements to Boolean 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)
   End Sub
End Module

' This example of the BitConverter.ToBoolean( Byte( ), Integer )
' method generates the following output. It converts elements of
' a Byte array to Boolean values.
' 
' initial Byte array
' ------------------
' 00-01-02-04-08-10-20-40-80-FF
' 
' index   array element   Boolean
' -----   -------------   -------
'     0              00     False
'     1              01      True
'     3              04      True
'     5              10      True
'     8              80      True
'     9              FF      True
// 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
*/

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.