BitConverter.ToBoolean Method
.NET Framework 4
Returns a Boolean value converted from one byte at a specified position in a byte array.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Byte[]
An array of bytes.
- startIndex
- Type: System.Int32
The starting position within value.
Return Value
Type: System.Booleantrue if the byte at startIndex in value is nonzero; otherwise, false.
| Exception | Condition |
|---|---|
| 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.