C# | BitConverter.ToBoolean() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Ouput:

Initial Array: 0 1 2 4 8 16 32 64 

startindex in less than zero
Exception Thrown: System.ArgumentOutOfRangeException

Example 3: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Ouput:

byte array is null
Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.