【问题标题】:Operations on BitArray [duplicate]BitArray 上的操作 [重复]
【发布时间】:2011-12-29 23:47:04
【问题描述】:

可能重复:
How i can convert BitArray to single int?

如何将整数读入BitArray(6)(假设可以包含)以及如何将BitArray(6) 转换为无符号/有符号整数。

【问题讨论】:

    标签: c# bitarray


    【解决方案1】:
    BitArray FromInt32(Int32 a)
    {
        byte[] bytes = BitConverter.GetBytes(a);
        return new BitArray(bytes);
    }
    

    对于前面提到的反向操作see this question

    【讨论】:

      【解决方案2】:
      byte[] bytes = { 0, 0, 0, 25 };
      
      // If the system architecture is little-endian (that is, little end first),
      // reverse the byte array.
      if (BitConverter.IsLittleEndian)
          Array.Reverse(bytes);
      
      int i = BitConverter.ToInt32(bytes, 0);
      Console.WriteLine("int: {0}", i);
      // Output: int: 25
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-19
        • 1970-01-01
        • 2016-09-06
        • 1970-01-01
        • 1970-01-01
        • 2018-09-05
        • 1970-01-01
        • 2018-02-27
        相关资源
        最近更新 更多