BitConverter.ToBoolean Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
ToBoolean(ReadOnlySpan<Byte>) |
Converts a read-only byte span to a Boolean value. |
ToBoolean(Byte[], Int32) |
Returns a Boolean value converted from the byte at a specified position in a byte array. |
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
Converts a read-only byte span to a Boolean value.
public:
static bool ToBoolean(ReadOnlySpan<System::Byte> value);
public static bool ToBoolean (ReadOnlySpan<byte> value);
static member ToBoolean : ReadOnlySpan<byte> -> bool
Public Shared Function ToBoolean (value As ReadOnlySpan(Of Byte)) As Boolean
Parameters
- value
- ReadOnlySpan<Byte>
A read-only span containing the bytes to convert.
Returns
A Boolean representing the converted bytes.
Exceptions
The length of value
is less than 1.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Standard | 2.1 |
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
Returns a Boolean value converted from the byte at a specified position in a byte array.
public:
static bool ToBoolean(cli::array <System::Byte> ^ value, int startIndex);
public static bool ToBoolean (byte[] value, int startIndex);
static member ToBoolean : byte[] * int -> bool
Public Shared Function ToBoolean (value As Byte(), startIndex As Integer) As Boolean
Parameters
- value
- Byte[]
A byte array.
- startIndex
- Int32
The index of the byte within value
to convert.
Returns
true
if the byte at startIndex
in value
is nonzero; otherwise, false
.
Exceptions
value
is null
.
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.
using namespace System;
int main()
{
// Define an array of byte values.
array<Byte>^ bytes = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };
Console::WriteLine("{0,5}{1,16}{2,10}\n", "index", "array element", "bool" );
// Convert each array element to a Boolean value.
for (int index = 0; index < bytes->Length; index++)
Console::WriteLine("{0,5}{1,16:X2}{2,10}", index, bytes[index],
BitConverter::ToBoolean(bytes, index));
}
// The example displays the following output:
// index array element bool
//
// 0 00 False
// 1 01 True
// 2 02 True
// 3 04 True
// 4 08 True
// 5 10 True
// 6 20 True
// 7 40 True
// 8 80 True
// 9 FF True
using System;
class Example
{
public static void Main( )
{
// Define an array of byte values.
byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };
Console.WriteLine("{0,5}{1,16}{2,10}\n", "index", "array element", "bool" );
// Convert each array element to a Boolean value.
for (int index = 0; index < bytes.Length; index++)
Console.WriteLine("{0,5}{1,16:X2}{2,10}", index, bytes[index],
BitConverter.ToBoolean(bytes, index));
}
}
// The example displays the following output:
// index array element bool
//
// 0 00 False
// 1 01 True
// 2 02 True
// 3 04 True
// 4 08 True
// 5 10 True
// 6 20 True
// 7 40 True
// 8 80 True
// 9 FF True
open System
// Define an array of byte values.
let bytes = [| 0uy; 1uy; 2uy; 4uy; 8uy; 16uy; 32uy; 64uy; 128uy; 255uy |]
printfn "%5s%16s%10s\n" "index" "array element" "bool"
// Convert each array element to a Boolean value.
for i = 0 to bytes.Length - 1 do
printfn $"{i,5}{bytes[i],16:X2}{BitConverter.ToBoolean(bytes, i), 10}"
// The example displays the following output:
// index array element bool
//
// 0 00 False
// 1 01 True
// 2 02 True
// 3 04 True
// 4 08 True
// 5 10 True
// 6 20 True
// 7 40 True
// 8 80 True
// 9 FF True
Module Example
Public Sub Main()
' Define an array of byte values.
Dim bytes() As Byte = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 }
Console.WriteLine("{0,5}{1,16}{2,10}\n", "index", "array element", "bool" )
' Convert each array element to a Boolean value.
For index As Integer = 0 To bytes.Length - 1
Console.WriteLine("{0,5}{1,16:X2}{2,10}", index, bytes(index),
BitConverter.ToBoolean(bytes, index))
Next
End Sub
End Module
' The example displays the following output:
' index array element bool
'
' 0 00 False
' 1 01 True
' 2 02 True
' 3 04 True
' 4 08 True
' 5 10 True
' 6 20 True
' 7 40 True
' 8 80 True
' 9 FF True
Remarks
To convert a Boolean value to its byte representation, call the GetBytes(Boolean) method.
See also
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: