[英]C# byte[] to List<byte[]>
I'm trying to get a byte[] array 'a' into a List 'b', but it's not working. 我正在尝试将一个byte []数组'a'放入List'b'中,但它不起作用。 Say I have this byte array 'a'.
假设我有这个字节数组'a'。
12344
23425
34426
34533
I would like to get it into a 4 item (# of rows) List , but this isn't working. 我想把它变成4项(行数)列表,但这不起作用。 (setting up intermediate byte[] then adding it)
(设置中间字节[]然后添加)
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[]> b = new List<byte[]>();
byte[] inter_byte= new byte[5];
for (int u=0; u<4; u++)
{
for (int p=0; p<5; p++)
{
inter_byte[u] = file[(5*u) + p];
}
b.Add(inter_byte);
}
What I'm getting is a List 4 rows long, but it is all the last row. 我得到的是List 4行长,但它是最后一行。 What's the best way to do this?
最好的方法是什么?
Your byte array is a reference type, which means changing it in each loop changes the data stored. 您的字节数组是引用类型,这意味着在每个循环中更改它会更改存储的数据。 Declaring it inside of each loop should work:
在每个循环中声明它应该工作:
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[]> b = new List<byte[]>();
for (int u=0; u<4; u++)
{
byte[] inter_byte= new byte[5];
for (int p=0; p<5; p++)
{
inter_byte[p] = a[(5*u) + p];
}
b.Add(inter_byte);
}
您需要在每次迭代中重新分配inter_byte
,否则它将被重用并且您正在替换行。
something like this should do it... (unless i misunderstood the question) 这样的事情应该做到......(除非我误解了这个问题)
List<byte[]> b = a.Select((by, i) => new { group = i / 5, value = by })
.GroupBy(item => item.group)
.Select(group => group.Select(v => v.value).ToArray())
.ToList();
groups the bytes into arrays of 5 into a list. 将字节分组为5的数组到列表中。
inter_byte
is a reference to an array of bytes. inter_byte
是对字节数组的引用。 You are only allocating the actual array of bytes once (with the new byte[5]
. You need to do that in your loop. 您只需要分配实际的字节数组(使用
new byte[5]
。您需要在循环中执行此操作。
ttry this : 这个:
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[] b = new List<byte[]>();
for (int u=0; u<4; u++)
{
byte[] inter_byte= new byte[5];
for (int p=0; p<5; p++)
{
inter_byte[u] = file[(5*u) + p];
}
b.Add(inter_byte);
}
var a = new byte[]
{
1, 2, 3, 4, 4,
2, 3, 4, 2, 5,
3, 4, 4, 2, 6,
3, 4, 5, 3, 3
};
var b = new List<byte[]>();
int groupSize = 5;
for (int i = 0; i < a.Length; i += groupSize)
{
int interSize = Math.Min(a.Length - i, groupSize);
var interByte = new byte[interSize];
Buffer.BlockCopy(a, i, interByte, 0, interSize);
b.Add(interByte);
}
Here's a nice extension method for what you want to do, but it's a bit safer because it won't run into out of range issues. 这是一个很好的扩展方法,你想要做什么,但它更安全一点,因为它不会遇到超出范围的问题。
public static IList<T[]> GroupArray<T>(this T[] array, int groupSize)
{
if (array == null)
throw new ArgumentNullException("array");
if (groupSize <= 0)
throw new ArgumentException("Group size must be greater than 0.", "groupSize");
IList<T[]> list = new List<T[]>();
T[] temp = new T[groupSize];
for (int i = 0; i < array.Length; i++)
{
if ((i % groupSize) == 0)
{
temp = new T[groupSize];
list.Add(temp);
}
temp[(i % groupSize)] = array[i];
}
return list;
}
SAMPLE USAGE: 样品使用:
Byte[] myByte = { 1, 2, 3, 4, 4, 2, 3, 4, 2, 5, 3, 4, 4, 2, 6, 3, 4, 5, 3, 3 };
IList<Byte[]> myList = myByte.GroupArray(5);
foreach (var item in myList)
{
Console.Write(item + " ");
foreach (var item2 in item)
{
Console.Write(item2);
}
Console.WriteLine();
}
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[]> b = new List<byte[]>();
for (int u=0; u<a.Count; u+=5)
{
b.Add(a.Skip(u).Take(5).ToArray());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.