Open In App

C# | BitConverter.ToBoolean() Method

Improve
Improve
Like Article
Like
Save
Share
Report

This method is used to return a Boolean value converted from the byte at a specified position in a byte array.

Syntax:

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

Parameters:

value: It is the required byte array.
startIndex: It is the index of the byte within value.

Return Value: This method returns true if the byte at startIndex in value is nonzero otherwise it will return false.

Exceptions:

  • ArgumentNullException: If the value is null.
  • ArgumentOutOfRangeException: If the startIndex is less than zero or greater than the length of value minus 1.

Below programs illustrate the use of BitConverter.ToBoolean(Byte[], Int32) Method:

Example 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();
    }
}


Output:

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

Example 2: For 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();
    }
}


Output:

Initial Array: 0 1 2 4 8 16 32 64 

startindex in less than zero
Exception Thrown: System.ArgumentOutOfRangeException

Example 3: For 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);
        }
    }
}


Output:

byte array is null
Exception Thrown: System.ArgumentNullException

Reference:



Last Updated : 16 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads