【问题标题】:Convert collection of 32 bool values to 32-bit integer [duplicate]将 32 个布尔值的集合转换为 32 位整数 [重复]
【发布时间】:2016-05-07 21:19:42
【问题描述】:

所以我是 C# 的新手,我已经搜索过,但我只是不知道我在搜索什么。

我有一个名为 inputs 的布尔变量,它从 ModBus 接口库中保存了 32 个真/假位。

这是一张解释我的 32 个真/假值的图片。基本上存储在输入中的 32 个真/假值。我需要将它们整理成一个 32 位整数。

我想知道的是如何将这 32 个值整理成一个 32 位字符串,即 (0000 0000 0000 1000 1000 1000 1000 1000),这显然是一个 32 位整数,并给我一个十进制数 88888。

我现在卡住了,因为我不确定我应该使用什么命令。

【问题讨论】:

  • 还有Int32UInt32?
  • 我刚刚添加了一个带有图像的编辑。这更清楚了吗?基本上那些 32 真/假存储在输入中。我需要将它们整理成一个 32 位整数。最后会给我一个数字。
  • 你想知道你有多少真假boelans? boelan 变量只能为真/假。如果您想知道有多少是真的,有多少是假的,那么您必须创建一个循环和两个称为“int AmountTrue”“int Amountfalse”的 int 变量,并根据循环目标向该变量添加值
  • 对不起,我不想知道有多少是真假。我希望能够将每 1 和 0 整理成一个 32 位字符串,即 (0000 0000 0000 1000 1000 1000 1000 1000),这显然是一个 32 位整数并给我一个十进制数 88888
  • 像往常一样,.NET 有一个类型。 BitVector32.

标签: c# type-conversion modbus modbus-tcp


【解决方案1】:

只是一些位操作:

uint i = 0;
var boolArr = new[] { true, false, true }; // 0b101
foreach (var bit in boolArr)
{
    // perform a bitwise left shift by 1 position
    // equivalent to multiplying i by 2
    i <<= 1;

    // store the bool value in the LSB of i
    i |= (uint)(bit ? 1 : 0);
}
Console.WriteLine(i);

将输出一个值5

或单线:

val = Convert.ToUInt32(string.Join("",
          bools.Select(b => b ? 1 : 0)), 2);

【讨论】:

  • 越来越近了。但是我收到一个错误Error CS0266 Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?) 如果我将 uint 更改为 int 它将起作用,但显然我最终会得到否定
  • @Bidaum92 什么错误?
  • 刚刚编辑了错误的评论
  • @Bidaum92 哦,是的,对不起,我最初错过了演员阵容。在我的代码的第 4 行添加 (uint) 强制转换。
【解决方案2】:

一旦你有一个以位为单位的字符串值,你只需要使用

转换该字符串
  int output = Convert.ToInt32(input, 2);

这里已经回答了。

Convert binary string into integer

【讨论】:

    【解决方案3】:

    我想这就是你想要的。 booleans 将是您的 bool 数组,然后 ints 使用 bool 数组的长度进行初始化(需要),因为它们是相同的。 i 只是我们在 foreach 循环中使用的计数器。然后,增加i 并将数组中的一个 bool 值转换为整数,并将其保存在 int 数组中。最后一行连接所有整数。在示例中,知道 True = 1 和 False = 0,你有 TFTF,那么,ints = 1, 0, 1, 0int 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# 课程,你会学到很多东西,无论如何,我希望这是你想要的,如果你想要一个整数数组,只需删除最后一行。下次请在帖子中包含更多信息。

    【讨论】:

    • 是的,我确实需要一个短期课程……这样我才能理解术语……然后才能更好地实际使用我的 google-fu!为缺乏信息道歉..再次是因为我缺乏知识。
    【解决方案4】:

    如果你想更好地理解(James 似乎是正确的),看看这个(你可以直接复制粘贴到 Linqpad 中,否则,删除 Dump() 调用,并使用 ToString()):

    // Initialize variables
    bool[] ba = new bool[24];
    Random r = new Random();
    StringBuilder sb = new StringBuilder();
    Int32 i32 =0;
    
    // Initialize array
    for (int i = 0; i < ba.Length; i++)
    {
        var d = r.Next(0,10);
        ba[i] = (d < 5) ? true : false;
    }
    
    // Dump to string: 
    for (int i = 0; i < ba.Length; i++)
    {
        sb.Append( ba[i] ? 1 : 0);
    }
    
    // Build your int
    for (int i = 0; i < ba.Length; i++)
    {
        // Shift left
        i32 = i32 << 1;
        // Add your current value
        i32+= ba[i] ? 1 : 0;
    }
    
    // Outputs
    ba.Dump("array output");
    sb.ToString().Dump("as a string");
    i32.Dump("as an int");
    

    以下内容也应该有帮助:<< Operator (C# Reference)this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2014-09-03
      • 2019-08-17
      • 2011-10-25
      • 2018-03-23
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多