uk3ndt
uk3ndt

Reputation: 65

Foreach output into a boolean array

I want to "save" my output from my foreach loop into a boolean array. What would be the easiest way to tackle that?

foreach(char est in resultss)
{
    
    int test = est - '0';
    tru = Convert.ToBoolean(test); // A Bool[] instead of Bool
    //  Console.WriteLine(tru);
}

I'm working on a binary clock and imp trying to convert a number like 22 into binary (done that) and than into a bool array.

Upvotes: 1

Views: 570

Answers (2)

Hiral Desai
Hiral Desai

Reputation: 1122

You could use LINQ for this, please add reference to System.Linq and do this.

var boolArray = resultss.Select(r => Convert.ToBoolean(r - '0')).ToArray();

Upvotes: 1

Wowo Ot
Wowo Ot

Reputation: 1529

 List<bool> list = new List<bool>();
 foreach(char est in resultss)
 {
     int test = est - '0';
     tru = Convert.ToBoolean(test);
     list.Add(tru);
 }

Upvotes: 1

Related Questions