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:
// 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
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:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Clone() Method
- C# | Uri.IsBaseOf(Uri) Method
- C# | Random.Next() Method
- C# | Uri.GetHashCode() Method
- C# | IndexOfAny() Method
- C# | Uri.IsHexDigit() Method
- C# | Uri.FromHex() Method
- C# | SortedDictionary.Add() Method
- C# | Dictionary.Add() Method
- C# | StartsWith() Method
- C# | CopyTo() Method
- C# | Replace() Method
- C# | Remove() Method
- C# | Math.Max() Method
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.
Improved By : nidhi_biet



