Share via


Multi-Dimensional Byte Array

Question

Tuesday, October 23, 2012 5:57 PM

What is the syntax that I need to follow to create a Multi-Dimensional array of bytes in C# ?

I have searched (at least) ten different pages (including these forums) on this site (and others) and I have yet to see one example of a two-dimensional array of bytes.

When I try to follow the syntax I see in other types (e.g., int) I get error messages.

Is this explained (with examples) in any of the video tutorials ?

While this idea works...

   byte[] Channel_01_Signals = new byte[768];
   byte[] Channel_02_Signals = new byte[768];
   byte[] Channel_03_Signals = new byte[768];

...it isn't very pretty (nor, I suspect, efficient)

Suggestions welcome

All replies (10)

Tuesday, October 23, 2012 6:52 PM ✅Answered | 1 vote

(1) Will I be able to do something like this ?

==============================CLIP============================

 Stadium_Seats[game][day][team][arena][section][row][seat_number]= AVAILABLE;    // This seat can now be sold

==============================CLIP============================

(2) Fix my thoughts early, please, can the other brackets contain any number I want ?

(3) Also, do I have to make all the indexes (indices?)  integers ? I'm pretty sure that that will work.

(4) By the way, is this the same thing as a "jagged array" ?

Answering your questions:

1. Yes.

2. Yes (but it better be smaller than the "top" value)

3. Yes. Arrays only accept Int32 indexes.

4. That you used is a jagged array, that means an array of arrays. A multidimensional array is a little different. Expl. below:

string[,] multiDim = new string[5, 3];
string[][] jagged = new string[5][]{new string[1]{""},
new string[1]{""}, new string[3]{"","",""}, new string[2]{""},new string[4]{"","","","hi"}};
};

multiDim[3, 2] = "Hi";
jagged[3][2] = Hello;

They might seem very similar, but while the first is an array that just happens to have 2 dimensions, the second is an array of arrays. Each element of the 'jagged' array is itself an array, so you may do something like this:

jagged[0] = new string[]{"Bye"};

while in the other case, each element is a string, that happens to have 2 "keys" or dimensions.

João Miguel


Tuesday, October 23, 2012 6:04 PM | 1 vote

Take a look at here Arrays Tutorial

Declaring Arrays

C# supports single-dimensional arrays, multidimensional arrays (rectangular arrays), and array-of-arrays (jagged arrays). The following examples show how to declare different kinds of arrays:

Single-dimensional arrays:

int[] numbers;

Multidimensional arrays:

string[,] names;

Array-of-arrays (jagged):

byte[][] scores;

Declaring them (as shown above) does not actually create the arrays. In C#, arrays are objects (discussed later in this tutorial) and must be instantiated. The following examples show how to create arrays:

Single-dimensional arrays:

int[] numbers = new int[5];

Multidimensional arrays:

string[,] names = new string[5,4];

Array-of-arrays (jagged):

byte[][] scores = new byte[5][];
for (int x = 0; x < scores.Length; x++) 
{
   scores[x] = new byte[4];
}

If you get your question answered, please come back and Mark As Answer.
Web Developer


Tuesday, October 23, 2012 6:41 PM

Aha ! So THAT'S the syntax...

byte[][] scores = new byte[5][];

Thanks for the clue !  You are the first to provide a concrete example.

So the magic trick is to place an integer inside the *first *set of brackets, but not the second.

Great, thanks.

I'm thinking of indexing, and I want to do a 3 or 4 dimensional array where the indexing does the magic tricks, e.g.,  in a loop of some sort, I'll do something like

The_Byte_I_Want = The_Big_Array[i][j][k][l];

That would open the door to a very good use of the machine, memory, and hopefully enough speed to turn this project into a real life product.

I just tried this, am I close ?

byte[][][][][][][] Stadium_Seats = new byte[87][][][][][][];

Will I be able to do something like this ?

==============================CLIP============================

 Stadium_Seats[game][day][team][arena][section][row][seat_number]= AVAILABLE;    // This seat can now be sold

==============================CLIP============================

Fix my thoughts early, please, can the other brackets contain any number I want ?

If so, that seems like it would invite some interesting mistakes.

Also, do I have to make all the indexes (indices?)  integers ? I'm pretty sure that that will work.

By the way, is this the same thing as a "jagged array" ?

I happen to know (or believe) what the exact size of this array will be.

Thanks again for the response.


Tuesday, October 23, 2012 6:51 PM

You can't put any number in DECLARATION of this jagged array, but you can put any, to get the specified ite,

array[0][0]; //as example
array[1][1];

And to initialize, you can do seomthing like this one

byte[][] Channel_01_Signals = new byte[1][] { new byte[] { 1, 2, 3 } };

If you get your question answered, please come back and Mark As Answer.
Web Developer


Tuesday, October 23, 2012 6:55 PM | 1 vote

(1) Will I be able to do something like this ?

==============================CLIP============================

 Stadium_Seats[game][day][team][arena][section][row][seat_number]= AVAILABLE;    // This seat can now be sold

==============================CLIP============================

(2) Fix my thoughts early, please, can the other brackets contain any number I want ?

(3) Also, do I have to make all the indexes (indices?)  integers ? I'm pretty sure that that will work.

(4) By the way, is this the same thing as a "jagged array" ?

Answering your questions:

1. Yes.

2. Yes (but it better be smaller than the "top" value)

3. Yes. Arrays only accept Int32 indexes.

4. That you used is a jagged array, that means an array of arrays. A multidimensional array is a little different. Expl. below:

string[,] multiDim = new string[5, 10];
string[][] jagged = new string[5][10];

multiDim[3, 2] = "Hi";
jagged[3][2] = Hello;

They might seem very similar, but while the first is an array that just happens to have 2 dimensions, the second is an array of arrays. Each element of the 'jagged' array is itself an array, so you may do something like this:

jagged[0] = new string[]{"Bye"};

while in the other case, each element is a string, that happens to have 2 "keys" or dimensions.

string[][] jagged = new string[5][10];

This code that you provide won't compile, but this

string[,] arr = new string[5,10];

If you get your question answered, please come back and Mark As Answer.
Web Developer


Tuesday, October 23, 2012 7:07 PM | 1 vote

Oops... You're right. I corrected it.

João Miguel


Tuesday, October 23, 2012 7:43 PM

Now this is even more helpful.

Thank you.

I missed that in the Tutorials as well.

Thank you; both of you.


Tuesday, October 23, 2012 7:55 PM

Bingo !

I think this is what I really want....

byte[,] Smaller_DataBase = new byte[20, 256];

byte[, ,] Big_Base = new byte[1, 20, 256];

Big_Base[0, 18, 207] = 0x2F;

Smaller_DataBase[4, 19] = Big_Base[0, 18, 207];

Just stepped through it. It appears that the number which I wanted appeared in the place where I wanted it.

So that's it, yes ? Empty commas on the left, integer values on the right. Yes ? No ?


Tuesday, October 23, 2012 7:55 PM

Question.....

Now, can multiple threads read from my big data base without started quarrels among themselves ?

The big database will be filled, after which a bunch of little threads will then be turned loose to take their own little piece of the big chunk.

As best I can plan at this moment, each thread will only read its little section of the bytes; three of them if I'm scheming properly.

None of the other threads will be interested in the other guy's data.

e.g., Channel 1 will fish out Channel 1 data, while Channel 2 only cares about the data for Channel 2, and the same thing for Channel 3, Channel 4, and so on.

While these channels will be threading themselves on their tasks, the data receiver will be filling another array for when they get done.

When they are all done, (according to theory) the data receiver will have another big one ready for them.

The process goes on and on, and the world is happy.


Tuesday, October 23, 2012 7:56 PM

Interesting observation; placing the value of 1 in a given dimension is, well, duh, I don't know how to phrase it.

Superfluous ?