Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
BitConverter.ToBoolean() Method in C#
The BitConverter.ToBoolean() method in C# returns a Boolean value converted from the byte at a specified position in a byte array. This method reads a single byte and returns true if the byte is non-zero, or false if the byte is zero.
Syntax
Following is the syntax −
public static bool ToBoolean(byte[] value, int startIndex);
Parameters
value − An array of bytes.
startIndex − The starting position within the byte array.
Return Value
Returns true if the byte at startIndex in the array is non-zero; otherwise, false.
Example
Let us see an example to implement the BitConverter.ToBoolean() method −
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 50, 100, 255 };
Console.WriteLine("Array values...");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("Index {0}: {1}", i, arr[i]);
}
Console.WriteLine("\nConverted boolean values...");
for (int index = 0; index < arr.Length; index++) {
bool res = BitConverter.ToBoolean(arr, index);
Console.WriteLine("Byte {0} -> {1}", arr[index], res);
}
}
}
The output of the above code is −
Array values... Index 0: 0 Index 1: 50 Index 2: 100 Index 3: 255 Converted boolean values... Byte 0 -> False Byte 50 -> True Byte 100 -> True Byte 255 -> True
Using BitConverter.ToBoolean() with Binary Data
Example
using System;
public class BooleanConverter {
public static void Main() {
// Create boolean values and convert to bytes
bool[] boolValues = { true, false, true };
Console.WriteLine("Original boolean values:");
foreach (bool b in boolValues) {
Console.WriteLine(b);
}
// Convert booleans to byte array
byte[] byteArray = new byte[boolValues.Length];
for (int i = 0; i < boolValues.Length; i++) {
byteArray[i] = BitConverter.GetBytes(boolValues[i])[0];
}
Console.WriteLine("\nConverted back to boolean:");
for (int i = 0; i < byteArray.Length; i++) {
bool converted = BitConverter.ToBoolean(byteArray, i);
Console.WriteLine("Byte {0} -> {1}", byteArray[i], converted);
}
}
}
The output of the above code is −
Original boolean values: True False True Converted back to boolean: Byte 1 -> True Byte 0 -> False Byte 1 -> True
Exception Handling
The method throws an ArgumentException if startIndex is greater than or equal to the length of the array −
using System;
public class ExceptionDemo {
public static void Main() {
byte[] arr = { 1, 2, 3 };
try {
// This will throw an exception
bool result = BitConverter.ToBoolean(arr, 5);
}
catch (ArgumentException ex) {
Console.WriteLine("Exception: " + ex.Message);
}
try {
// Valid conversion
bool result = BitConverter.ToBoolean(arr, 1);
Console.WriteLine("Valid conversion: " + result);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Exception: startIndex is greater than or equal to the length of value minus 0. Valid conversion: True
Conclusion
The BitConverter.ToBoolean() method converts a byte at a specified index to a boolean value, returning true for any non-zero byte and false for zero. This method is useful when working with binary data or when converting between different data representations in C# applications.
