2

I'm trying to get a byte[] array 'a' into a List 'b', but it's not working. Say I have this byte array 'a'.

12344
23425
34426
34533

I would like to get it into a 4 item (# of rows) List , but this isn't working. (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. What's the best way to do this?

0

8 Answers 8

6

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);
 }        
Sign up to request clarification or add additional context in comments.

Comments

5

You need to reallocate inter_byte in each iteration, otherwise it's getting reused and you're replacing the rows.

Comments

3

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.

1 Comment

I like yours better than mine. :)
1

inter_byte is a reference to an array of bytes. You are only allocating the actual array of bytes once (with the new byte[5]. You need to do that in your loop.

Comments

1

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();
        }

Comments

0

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);
}

Comments

0
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);
}

Comments

0
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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.