c# - 在c#中将0和1的数组转换为int

标签 c#

我有以下内容:

public int [] PermuteNum(int num)
{
int[] newArray = new int[16];
string strNum = Convert.ToString(num, 2);
int[] bits = strNum.PadLeft(16, '0').Select(c => int.Parse(c.ToString())).ToArray();

for (int i = 0; i < bits.Length; i++)
{

int newBit = P(i);
int NewNum = bits[newBit];
newArray[i] = NewNum;

}

return newArray;
}

如何将这个由 1 和 0 组成的数组转换回我的初始 int?第一个元素是最高有效位。

最佳答案

int result = 0;
for (int i = 0; i < newArray.Length; i++)
{
    result *= 2;
    result += newArray[i];
}

关于c# - 在c#中将0和1的数组转换为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426940/

相关文章:

c# - 如何重新启动统一动画师?

c# - 如何避免将 iQueryable 转换为列表?

c# - Floyd-Warshall 算法 - 还获取除最短距离以外的每个点的名称

c# - 数组中等于 N 的 K 个元素之和

c# - WPF ListBox,自动添加 "new element"行

c# - 对不同的 Windows 窗体控件使用相同的事件处理程序是否安全?

c# - 将值返回到主 C#

C# SMO 创建外部表

c# - 为什么要在 RabbitMQ 中声明 Exchange?

c# - 无法加载文件或程序集 'Microsoft.Practices.EnterpriseLibrary.Common' 或其依赖项之一