当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# BitConverter.ToBoolean()用法及代码示例


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

用法:

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

参数:


  • value:它是必需的字节数组。
    startIndex:它是值中字节的索引。

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

异常:

  • ArgumentNullException:如果值为空。
  • 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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | BitConverter.ToBoolean() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。