【问题标题】:Linq to convert bool[] to byte[]Linq 将 bool[] 转换为 byte[]
【发布时间】:2023-03-03 07:20:20
【问题描述】:

我有一个数组中的布尔值列表

bool[] items = { true, true, true, true, true, true, true, true,  //#1
                 false, false, false, false, false, false, false, true,  //#2
                 false, false, false, false, false, false, true , true  //#3
               };

linq 有没有一种简单的方法可以将其转换为byte[]

//expected result
byte[] result = { 255, 1, 3 };

【问题讨论】:

  • 我的错,虽然这是另一个简单问题。
  • Here几乎重复的答案。虽然您将 转换为布尔值,但存在更简单的答案。
  • “linq 有没有简单的方法来转换这个” 简单是相对的和主观的。我会说不。当然可以。为什么这么坚持LINQ?你可能可以用沙拉钳盖房子,但为什么呢?使用任何使您的代码最容易阅读的东西。

标签: c# arrays linq byte bit


【解决方案1】:

“linq 有没有一种简单的方法可以将其转换为byte[]?”我不知道我是否认为它很简单,而且它肯定不漂亮,但这似乎有效:

        byte[] result = Enumerable.Range(0, items.Length / 8)
            .Select(i => (byte)items.Select(b => b ? 1 : 0)
                              .Skip(i * 8)
                              .Take(8)
                              .Aggregate((k, j) => 2 * k + j))
            .ToArray();

【讨论】:

    【解决方案2】:

    因此,您有一个布尔值序列,并且您希望将每 8 个连续布尔值的值转换为一个字节,其中位模式等于首先具有最高有效位 (MSB) 的 8 个布尔值。

    See how to convert an sequence of 8 Booleans into one byte

    转换分两步完成:

    • 将序列分成 8 个布尔值组
    • 将每组 8 个布尔值转换为一个字节

    为了保持可读性,我创建了 IEnumerable 的扩展函数。见Extension methods demystified

    static class EnumerableExtensions
    {
    
        public static IEnumerable<Byte> ToBytes(this IEnumerable<Bool> bools)
        {
            // converts the bools sequence into Bytes with the same 8 bit
            // pattern as 8 booleans in the array; MSB first
            if (bools == null) throw new ArgumentNullException(nameof(bools));
    
            // while there are elements, take 8, and convert to Byte
            while (bools.Any())
            {
                IEnumerable<bool> eightBools = bools.Take(8);
                Byte convertedByte = eightBools.ToByte();
                yield return convertedByte();
    
                 // remove the eight bools; do next iteration
                 bools = bools.Skip(8);
            }
        }
    
        public static Byte ToByte(this IEnumerable<bool> bools)
        {    // converts the first 8 elements of the bools sequence
             // into one Byte with the same binary bit pattern
             // example: 00000011 = 3
    
             if (bools == null) throw new ArgumentNullException(nameof(bools));
             var boolsToConvert = bools.Take(8);
    
             // convert
             byte result = 0;
             int index = 8 - source.Length;
             foreach (bool b in boolsToConvert)
             {
                 if (b)
                     result |= (byte)(1 << (7 - index));
                 index++;
             }
             return result;
        }
    }
    

    用法如下:

    IEnumerable<bool> items = ...
    IEnumerable<Byte> convertedItems = items.ToBytes();
    

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 2012-02-17
      • 1970-01-01
      • 2011-06-08
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多