C# 中的 BitConverter.ToBoolean() 方法

csharpserver side programmingprogramming更新于 2025/9/19 18:17:00

C# 中的 BitConverter.ToBoolean() 方法返回从字节数组中指定位置的字节转换而来的布尔值。

语法

以下是语法 −

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

上面,arr 是一个字节数组,而 startIndex 是值中字节的索引。

示例

现在让我们看一个实现 BitConverter.ToBoolean() 方法 −

using System;
public class Demo {
   public static void Main(){
      byte[] arr = { 50, 100 };
      Console.WriteLine("数组值...");
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine("{0} ", arr[i]);
      }
      Console.WriteLine("
转换后的值...");       for (int index = 0; index < arr.Length; index++) {          bool res = BitConverter.ToBoolean(arr, index);          Console.WriteLine(""+res);       }    } }

输出

将产生以下输出 −

数组值...
50
100
转换后的值...
True
True

相关文章