我想这就是你想要的。booleans
将是你的 bool 数组,然后ints
,用 bool 数组的长度初始化(需要),因为它是相同的。i
只是我们在 foreach 循环中使用的计数器。i
然后,将数组中的一个 bool 值递增并将其转换为整数,并将其保存在 int 数组中。最后一行连接所有整数。在示例中,知道 True = 1 和 False = 0,您有 TFTF,然后,ints = 1, 0, 1, 0
并且int result = 1010
bool[] booleans = { true, false, true, false }; // Your bool array
int[] ints = new int[booleans.Length]; // Integer equivalent of "booleans"
int i = -1; // Counter for "foreach" loop
foreach (bool boo in booleans)
{
i++;
ints[i] = Convert.ToInt32(boo); // Convert boolean into binary
}
int result = Convert.ToInt32(string.Join("", ints)); // Concatenate them
我建议你参加一个简短的 C# 课程,你会学到很多东西,无论如何,我希望这是你想要的,如果你想要一个整数数组,只需删除最后一行。请下次,在帖子中包含更多信息。