BitConverter.ToBoolean 方法

定义

重载

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.

ToBoolean(ReadOnlySpan<Byte>)

将只读字节范围转换为布尔值。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

参数

value
ReadOnlySpan<Byte>

包含要转换的字节的只读范围。A read-only span containing the bytes to convert.

返回

表示已转换字节的布尔值。A Boolean representing the converted bytes.

例外

value 的长度小于 0。The length of value is less than 0.

ToBoolean(Byte[], Int32)

返回由字节数组中指定位置的一个字节转换来的布尔值。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

参数

value
Byte[]

字节数组。A byte array.

startIndex
Int32

value 中的字节索引。The index of the byte within value.

返回

如果 startIndex 中的 value 处的字节非零,则为 true;否则为 falsetrue if the byte at startIndex in value is nonzero; otherwise, false.

例外

valuenullvalue is null.

startIndex 小于零或大于 value 减 1 的长度。startIndex is less than zero or greater than the length of value minus 1.

示例

下面的代码示例通过 ToBoolean 方法将 Byte 数组的元素转换为 Boolean 值。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
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

注解

若要将 Boolean 值转换为其字节表示形式,请调用 GetBytes(Boolean) 方法。To convert a Boolean value to its byte representation, call the GetBytes(Boolean) method.

另请参阅

适用于