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
)
public static boolean ToBoolean (
	byte[] value, 
	int startIndex
)
public static function ToBoolean (
	value : byte[], 
	startIndex : int
) : boolean

Parameters

value

An array of bytes.

startIndex

The starting position within value.

Return Value

true if the byte at startIndex in value is nonzero; otherwise, false.

Exception typeCondition

ArgumentNullException

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

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

// Example of the BitConverter.ToBoolean method.
import System.*;

class GetBytesBoolDemo
{
    private static String formatter = "{0,5}{1,16}{2,10}";

    // Convert a byte array element to bool and display it.
    public static void BAToBool(ubyte bytes[], int index)
    {
        boolean value = BitConverter.ToBoolean(bytes, index);
        Console.WriteLine(formatter, (Int32)index, 
            BitConverter.ToString(bytes, index, 1), (System.Boolean)value);
    } //BAToBool

    public static void main(String[] args)
    {
        ubyte byteArray[] =  { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };

        Console.WriteLine(("This example of the "
            + "BitConverter.ToBoolean( byte[ ], int ) \n"
            + "method 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);
    } //main
} //GetBytesBoolDemo

/*
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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft