this is an easy one

N

news

I must be tired.

I have a two dimensional byte array

byte[,] byte_array = new byte[12,256];

Now I populate the 12 arrays each with 256 byte values.

I create a single dimension array

byte[] single_byte_array = new byte[256];

How do I copy one of the 256 byte arrays from the 2 dimension array
byte_array to single_byte_array?

This doesn't work

single_byte_array = byte_array[1];

So can it be done without iterating through all 256 characters?


Thank You,
Bill
 
C

Cowboy \(Gregory A. Beamer\)

Are you ever using the array as a Jagged array or are you merely cataloging
12 arrays? If you simply are using the byte arrays, why not use some other
type of collection of arrays? Much nicer and easier to work with. Make
sense? If not, write back.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
 
P

Peter Duniho

I have a two dimensional byte array

byte[,] byte_array = new byte[12,256];

Now I populate the 12 arrays each with 256 byte values.

I create a single dimension array

byte[] single_byte_array = new byte[256];

How do I copy one of the 256 byte arrays from the 2 dimension array
byte_array to single_byte_array?

As near as I can tell, you can only copy between arrays with the same rank
(same number of dimensions). So, no...you can't declare a two-dimensional
array and then copy from that to a one-dimensional array without iterating
through all array elements you want to copy.

That said:

I don't think it should be that big of a problem to do the iteration. I
presume that the Array.Copy() method is optimized to deal with larger
chunks of memory, but unless you have to do this a lot, and with a lot of
data, it's probably not a big deal to not be able to take advantage of
that.

Also, you could declare a one-dimensional array of one-dimensional arrays,
and then you would be able to do the copy, since you'd just select the
one-dimensional array you want to copy, and copy the whole thing. So your
code might look something like this:

byte[][] byte_array = new byte[12][256];
byte[] single_byte_array = new byte[256];

// make sure byte_array is initialized, of course
byte_array[1].CopyTo(single_byte_array, 0);

Pete
 
J

Jon Skeet [C# MVP]

news said:
I must be tired.

I have a two dimensional byte array

byte[,] byte_array = new byte[12,256];

Now I populate the 12 arrays each with 256 byte values.

You don't have 12 arrays there - you have *one* array. If you want
twelve arrays, you'll need to do:

byte[][] byteArrays = new byte[12][];

That's actually an array of 12 byte array references, all of which will
be null to start with.
I create a single dimension array

byte[] single_byte_array = new byte[256];

How do I copy one of the 256 byte arrays from the 2 dimension array
byte_array to single_byte_array?

Given the above, you could do singleByteArray = byteArrays; as you
wanted - but be aware that that doesn't copy the array, it just sets
the singleByteArray variable's value to be the same reference as
byteArrays - so changing the array via either variable will make the
change visible through the other variable too (because there's just the
one array involved).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top