0

i have tried to split a ByteArray (that represents a screenShot Data) into a 2d array so if i have for example an array of 3x3 (xy /rows columns)

it would look like this

int screenSwidth = 3;
int screenSheight = 3;
byte[] ScreenShot = new byte[] {1,2,3,4,5,6,7,8,9};

i am trying via 2 nested for loops, to split the serial data into separated rows in a new 2dimentional array as it was shaped/formed originaly on the screen.

lets Just call the new ScreenShotRows[] as "b" just for this example

b[0][0] = 1 b[0][1] = 2 b[0][2] = 3

b[1][0] = 4 b[1][1] = 5 b[1][2] = 6

b[2][0] = 7 b[2][1] = 8 b[2][2] = 9

and the question is what is the right way to iterate through the whole array i was trying to achieve that via this code .

     for (int HeightIter= 0; HeightIter < screenSheight; HeightIter++)
     {
           for (int WidthIter = 0; WidthIter < screenSwidth ; WidthIter++)
                {
                     ScreenShotRows[HeightIter, WidthIter] = ScreenShot[WidthIter];
                }
     }

and it loops over the first row , asigning the values :

b[0][0] = 1 b[0][1] = 2 b[0][2] = 3

b[1][0] = 1 b[0][1] = 2 b[0][2] = 3

b[2][0] = 1 b[0][1] = 2 b[0][2] = 3

this is my first attempt on this kind of multiDimetional /jagged array with nested for loop, and i found it very confiusing to make it work

also to get it done as fast as it could be,

cause my data is much larger than 3x3 , and method is called multiple times frequently so performance is crucial

my next move will be to have same way with the columns

elements 0,0 to 0,2 ---- elements 1,0 to 1,2 ---- ements 2,0 to 2,2

      147                    258                369

1 Answer 1

2

Replace your firsts line to:

for (int HeightIter= 0; HeightIter < screenSheight; HeightIter++)

And the line ScreenShotRows[HeightIter, WidthIter] = ScreenShot[WidthIter]; to

ScreenShotRows[HeightIter, WidthIter] = ScreenShot[3*HeightIter+WidthIter];

This should make 2d array.

Sign up to request clarification or add additional context in comments.

6 Comments

i've renamed it here so it's only a typo . i've fixed it now , that's not what is wrong though
Just checked my code with debugger - it works. How do you know how your 2D array looks like? Via debugger?
Rather than hard coding the 3, you should probably use screenSwidth. Magic numbers are bad.
yea , only that way i learn , i've checked it with microscope(debugger) and for the 3 , i was wondering which parameter to use while i was reading then refreshed the page and u made your comment on this , i'll give it a try , Thanks a lot Seeker +Servy
@servy indeed. Thanks for critic.
|

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.