 # [SOLVED] comparing two dimensional byte array

i am having a byte array let us say

byte arr={123456};

how to compare this array with a list of of other array like this for example

byte buffers = {123456,654321,987654,456789,765432};

if this method is wrong how can i declare that ?

any help?

``````You could use two for loops to compare your 5x7 array to your 7 array

the outer for loop would go from 0 to 4 and the control variable would be i
the inner loop would go from 0 to 6 and the control variable would be j

for (i=0; i <4; i++)
for (j=0; j<6 ; j++)
if array[j] !=buffer[i,j]
break;
else (j=6){
success=1;
print('its a match);println(buffer[i,j]);  //or whatever you want to do
}
}

if (success==0)
println("not a match");
``````

noweare:

``````You could use two for loops to compare your 5x7 array to your 7 array
``````

the outer for loop would go from 0 to 4 and the control variable would be i
the inner loop would go from 0 to 6 and the control variable would be j

for (i=0; i <4; i++)
for (j=0; j<6 ; j++)
if array[j] !=buffer[i,j]
break;
else (j=6){
success=1;
print('its a match);println(buffer[i,j]);  //or whatever you want to do
}
}

if (success==0)
println(“not a match”);

will try it

thank u

Kind of sticks-and-stones but it seems to work:

``````#define NUM_COMPARES    5
#define NUM_ELEMENTS    6

byte arr[NUM_ELEMENTS]={1,2,3,4,5,6};

byte  buffers[NUM_COMPARES][NUM_ELEMENTS] =
{
{7,6,5,4,3,2},
{9,8,7,6,5,4},
{1,2,3,4,5,6},
{4,5,6,7,8,9},
{7,6,5,4,3,2}
};

void setup()
{
bool
bMatch;
int
i, j;

Serial.begin(9600);

i = 0;
do
{
bMatch = true;
for( j=0; j<NUM_ELEMENTS; j++ )
if( arr[j] != buffers[i][j] )
bMatch = false;

if( !bMatch )
i++;

}while( i < NUM_COMPARES && !bMatch );

if( bMatch )
{
Serial.print( "Match found at buffer[" );
Serial.print(i);
Serial.println( "]" );
}
else
Serial.println( "No match found." );

}//setup

void loop()
{
}
``````

(I see I’m a little slow at typing. Will post anyway…)

You don’t say what you want to compare exactly. That if any of the arrays in the buffer match with the array? For that you could do:

``````bool is_match=false;
for(unsigned i=0; i<5; ++i)
if(memcmp(arr, buffers[i], sizeof(arr))==0)
{
is_match=true;
break;
}
``````

JarkkoL:
You don’t say what you want to compare exactly. That if any of the arrays in the buffer match with the array? For that you could do:

``````bool is_match=false;
``````

for(unsigned i=0; i<5; ++i)
if(memcmp(arr, buffers[i], sizeof(arr))==0)
{
is_match=true;
break;
}

yes this is what i want to do

thank u very much

Blackfin:
Kind of sticks-and-stones but it seems to work:

``````#define NUM_COMPARES    5
``````

#define NUM_ELEMENTS    6

byte arr[NUM_ELEMENTS]={1,2,3,4,5,6};

byte  buffers[NUM_COMPARES][NUM_ELEMENTS] =
{
{7,6,5,4,3,2},
{9,8,7,6,5,4},
{1,2,3,4,5,6},
{4,5,6,7,8,9},
{7,6,5,4,3,2}
};

void setup()
{
bool
bMatch;
int
i, j;

Serial.begin(9600);

i = 0;
do
{
bMatch = true;
for( j=0; j<NUM_ELEMENTS; j++ )
if( arr[j] != buffers[i][j] )
bMatch = false;

if( !bMatch )
i++;

}while( i < NUM_COMPARES && !bMatch );

if( bMatch )
{
Serial.print( “Match found at buffer[” );
Serial.print(i);
Serial.println( “]” );
}
else
Serial.println( “No match found.” );

}//setup

void loop()
{
}

``````

(I see I'm a little slow at typing. Will post anyway...)
``````

very nice code
it is working perfectly !!!
thank u very much for ur help

thank u very much to all of u

this is the final code that i used

``````#define NUM_COMPARES    5
#define NUM_ELEMENTS    6

bool bMatch;
int i, j;

byte arr[NUM_ELEMENTS]={7,2,3,4,5,6};

byte  buffers[NUM_COMPARES][NUM_ELEMENTS] =
{
{7,6,5,4,3,2},
{9,8,7,6,5,4},
{4,5,6,7,8,9},
{7,6,5,4,3,2},
{7,2,3,4,5,6}
};

void setup()
{
Serial.begin(9600);
}

void loop()
{  i = 0;
do
{
bMatch = true;
for( j=0; j<NUM_ELEMENTS; j++ )
if( arr[j] != buffers[i][j] )
bMatch = false;
if( !bMatch )
i++;
}

while( i < NUM_COMPARES && !bMatch );

if( bMatch )
{
Serial.print( "Match found at buffer[" );
Serial.print(i);
Serial.println( "]" );
}
else
Serial.println( "No match found." );

}
``````