C# 将 bool 数组转换为 int 的方法
以下是一个使用 C# 编写的将 bool 数组转换为 int 的程序示例:
public static int BoolArrayToInt(bool[] boolArray)
{
int result = 0;
for (int i = 0; i < boolArray.Length; i++)
{
if (boolArray[i])
{
result += (int)Math.Pow(2, i);
}
}
return result;
}
该方法接受一个 bool 数组作为输入并返回一个整数,该整数表示将该 bool 数组转换为二进制后所代表的十进制数。在方法中,我们使用一个循环来遍历 bool 数组的每个元素,并在每个元素为 true 时将对应的二进制位加入结果中。我们使用 Math.Pow 方法来计算 2 的幂次方,以便计算每个二进制位的值。最后,我们返回结果。
原文地址: https://www.cveoy.top/t/topic/nGqH 著作权归作者所有。请勿转载和采集!