我有一个数组中的布尔值列表
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 };
答案 0 :(得分:2)
“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();
答案 1 :(得分:1)
所以你有一系列布尔值,并且你希望将每8个连续布尔值的值转换成一个字节,其中位模式等于8个布尔值,最高位(MSB)优先。
See how to convert an sequence of 8 Booleans into one byte
转换分两步完成:
为了保持可读性,我创建了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();