Export (0) Print
Expand All

BitConverter.ToBoolean Method

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 GetBytesBoolDemo
{
    const string formatter = "{0,5}{1,16}{2,10}";

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

        Console.WriteLine( formatter, index, 
            BitConverter.ToString( bytes, index, 1 ), value );
    }

    public static void Main( )
    {
        byte[ ] byteArray = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };

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

        // Convert byte array elements to bool values.
        BAToBool( byteArray, 0 );
        BAToBool( byteArray, 1 );
        BAToBool( byteArray, 3 );
        BAToBool( byteArray, 5 );
        BAToBool( byteArray, 8 );
        BAToBool( 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
*/

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft