View previous topic :: View next topic |
Author |
Message |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
How do you check the equality of two byte arrays |
Posted: Mon Jan 17, 2005 7:52 am |
|
|
I'd like to check whether two arrays are exactly equal (same values in indeces) but the following does not seem to work
byte ONE[5];
byte TWO[5];
.....
....
....
...
if (ONE==TWO)
Success;
This seems to check whether the two have the same address and not the actual values. Do I have to run them through a for loop, is there no other way? |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jan 17, 2005 8:19 am |
|
|
Use a loop. If the data is a string, use STRNCMP() |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Jan 17, 2005 8:31 am |
|
|
or use: memcmp(ONE, TWO, sizeof(ONE)); |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jan 17, 2005 8:36 am |
|
|
oops I looked for it in the index of the help file. I didn't see it but do see it listed in the list of standard string functions. |
|
![](templates/subSilver/images/spacer.gif) |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
Memcmp doesn't seem to be defined... |
Posted: Tue Jan 18, 2005 12:54 am |
|
|
It doesn't look like memcmp exists, although it shows in the help file. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jan 18, 2005 2:00 am |
|
|
You'll have to include string.h
It is missing from some of the help files, but it is mentioned in the Aug-2004 pdf-fille.
A tip: Study the supplied header files, a lot of goodies can be found there.
Please note that the length parameter is an int8, so a maximum length of 255 can be compared. |
|
![](templates/subSilver/images/spacer.gif) |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
Tried it but somehow doesn't work |
Posted: Tue Jan 18, 2005 2:17 am |
|
|
Here's the code , what am I missing?
byte Master[8];
byte buffer[8];
...
...
...
...
...
for (i=0;i<8;i++)
Master[i]=buffer[i];
if (memcmp(buffer,master,sizeof(buffer))==0)
Beep_ok(); |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jan 18, 2005 3:03 am |
|
|
Looks fine to me. What is your problem? |
|
![](templates/subSilver/images/spacer.gif) |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
It doesn't beep OK |
Posted: Tue Jan 18, 2005 3:18 am |
|
|
No Beep |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jan 18, 2005 3:42 am |
|
|
You didn't mention your compiler version, please do so next time.
I compared string.h v3.187 to v3.216 and it seems they fixed a bug somewhere between those versions.
In memcmp(), change
to |
|
![](templates/subSilver/images/spacer.gif) |
picker
Joined: 07 Sep 2003 Posts: 19 Location: South Africa
|
|
Posted: Tue Jan 18, 2005 4:00 am |
|
|
Fantastic! Thanks, I'm still running 3.180. I'm too scared to upgrade to a newer version (although I download them) , because everything seems to be working (until I got to memcmp!) and my motto is if its not broken don't fix it...
Thanks for your help ckielstra! Much appreciated...
Regards
Picker |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Jan 18, 2005 2:57 pm |
|
|
picker wrote: | Fantastic! Thanks, I'm still running 3.180. I'm too scared to upgrade to a newer version (although I download them) , because everything seems to be working (until I got to memcmp!) and my motto is if its not broken don't fix it...
Thanks for your help ckielstra! Much appreciated...
Regards
Picker |
very often it is broken, just you don't know it, until you stumble on a bug. |
|
![](templates/subSilver/images/spacer.gif) |
.... Guest
|
|
![](templates/subSilver/images/spacer.gif) |
|