extracting from arrays

Sep 21, 2008 at 8:30pm
This is what I am looking to do.
I have this array where there's say 512 bytes (512 char arrray).
I need to extract 32 bytes from it and send it elsewhere. So basically I need a way to keep a track of 0-32 bytes, then move to 33-64 and so on.
I do know the length of the array or can get it from the first 32 bytes.

So here's my logic:

- Create a pointer (ptr) to point to start of the array that holds the data.
- Use a for loop
for( x=0;x<=511;x++0
- Inside use another for loop
for ( i=0;i<=31;i++)
In here I use a pointer to copy data to another 32 byte arrayx
arrayx[i]=*ptr++;
- if i = 32, then I send 32 bytes to the destination and x=i.
SO this would make x =32 and get into the outer for loop.

Now I need to copy from 33rd byte of the initial array to 64 and so on. But I am kinda getting stuck/confused at how to proceed.
WOuld anyome be able to suggest a better or kinda complete this logic ?
Thanks for all your help.






Sep 21, 2008 at 8:53pm
The range 0-32 is 33 bytes long, not 32.
Just use x+=32 as the increment, i<32 as the while condition, and access elements as array[x+i]. That's the second briefest method I can think of. The briefest method I can think of is using memcpy().
Sep 21, 2008 at 9:03pm
yea, dunt bother about the 32..33 stuff. Just wanted to run the logic by some people. Lemme also think about memcpy.
Somehow I don't use while loops often enough :)
Thanks anyways.
Sep 21, 2008 at 9:16pm
Um... No, I didn't tell you to use a while loop. That's what I call the condition for the for, since the loop will continue *while* the condition is true.
Sep 22, 2008 at 3:33am
You don't need nested for loops or pointers. Just transfer the first 32 elements of array a to array b.
e.g.
1
2
3
4
char a[512]={a,b,c,.......etc};
char b[512]={0};
for (int i=0;i<32;i++)
   b[i]= a[i];
Sep 22, 2008 at 8:36am
Ah, but he also needs to get bytes 32-63, 64-95, etc.
Sep 22, 2008 at 5:45pm
This is how I got it done:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

   while (i<=max length)  // say temp[150]
    {
    	for (int x=0;x<=31;x++)
    	{
    		buffer_out[x+i] = temp[x+i];  // copy into buffer_out
    	}

    	if (x==32)
    	{
    		i=x;   // i = 32 when loop exits
    		fpga_recv(); // call whatever function I need to.
    	}


    } // while loop ends
}



SO now I can keep adding 32 bytes at a time, send it over to where ever I need to :)
If you find any issues with it lemme know.
Last edited on Sep 22, 2008 at 5:46pm
Sep 22, 2008 at 6:05pm
The if is not only redundant, it also doesn't work with the standard.
i=x should be i+=x.
Sep 22, 2008 at 6:42pm
Thanks for point the i+ =x part. I missed it.

Topic archived. No new replies allowed.