如何将 IEnumerable 转换为字节数组
How to convert IEnumerable to byte array
我需要将多个数组合并为一个。我发现这似乎是一个很好的方法:
IEnumerable<byte> Combine(byte[] a1, byte[] a2, byte[] a3)
{
foreach (byte b in a1)
yield return b;
foreach (byte b in a2)
yield return b;
foreach (byte b in a3)
yield return b;
}
不过,我对IEnumerable
不是很熟悉。如何将结果转换回 byte[]
以便我可以进一步使用它?
谢谢。
而不是迭代它们只是 linq 的 .Concat
:
var joint = a1.Concat(a2).Concat(a3);
如果你想return它作为一个数组:
joint.ToArray();
我需要将多个数组合并为一个。我发现这似乎是一个很好的方法:
IEnumerable<byte> Combine(byte[] a1, byte[] a2, byte[] a3)
{
foreach (byte b in a1)
yield return b;
foreach (byte b in a2)
yield return b;
foreach (byte b in a3)
yield return b;
}
不过,我对IEnumerable
不是很熟悉。如何将结果转换回 byte[]
以便我可以进一步使用它?
谢谢。
而不是迭代它们只是 linq 的 .Concat
:
var joint = a1.Concat(a2).Concat(a3);
如果你想return它作为一个数组:
joint.ToArray();