📜  C#| BitConverter.ToBoolean()方法

📅  最后修改于: 2021-05-29 13:56:58             🧑  作者: Mango

此方法用于返回从字节数组中指定位置的字节转换而来的布尔值。

句法:

public static bool ToBoolean (byte[] value, int startIndex);

参数:

返回值:如果value中startIndex处的字节为非零,则此方法返回true,否则将返回false。

例外情况:

  • ArgumentNullException:如果该值为null。
  • ArgumentOutOfRangeException:如果startIndex小于零或大于值的长度减去1。

下面的程序说明了BitConverter.ToBoolean(Byte [],Int32)方法的用法:

范例1:

// C# program to demonstrate
// BitConverter.ToBoolean(bytes, index));
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Define an array of byte values.
            byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 };
  
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(bytes);
  
            for (int index = 0; index < bytes.Length; index++) {
  
                bool values = BitConverter.ToBoolean(bytes, index);
                Console.WriteLine("index     element           bool");
                Console.WriteLine(" {0}         {1}                   {2}",
                                         index, bytes[index], values);
            }
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
}
输出:
Initial Array: 0 1 2 4 8 16 32 64 

index     element           bool
 0         0                   False
index     element           bool
 1         1                   True
index     element           bool
 2         2                   True
index     element           bool
 3         4                   True
index     element           bool
 4         8                   True
index     element           bool
 5         16                   True
index     element           bool
 6         32                   True
index     element           bool
 7         64                   True

示例2:对于ArgumentOutOfRangeException

// C# program to demonstrate
// BitConverter.ToBoolean(bytes, index));
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Define an array of byte values.
            byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 };
  
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(bytes);
  
            Console.WriteLine("startindex in less than zero");
            bool values = BitConverter.ToBoolean(bytes, -1);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
}

输出:

Initial Array: 0 1 2 4 8 16 32 64 

startindex in less than zero
Exception Thrown: System.ArgumentOutOfRangeException

示例3:对于ArgumentNullException

// C# program to demonstrate
// BitConverter.ToBoolean(bytes, index));
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Define an array of byte values.
            byte[] bytes = null;
  
            // Getting bool value
            Console.WriteLine("byte array is null");
            bool values = BitConverter.ToBoolean(bytes, 0);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

输出:

byte array is null
Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.bitconverter.toboolean?view=netframework-4.7.2