View previous topic :: View next topic |
Author |
Message |
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Decompicating an array |
Posted: Tue May 23, 2006 7:48 am |
|
|
A silly question I'm sure.
How can I decomplicate this line.
Code: | if ((arr_usb_data_in[0]==0x03) &&
(arr_usb_data_in[1]==0x72) &&
(arr_usb_data_in[2]==0x65) &&
(arr_usb_data_in[3]==0x73) &&
(arr_usb_data_in[4]==0x65) &&
(arr_usb_data_in[5]==0x74) &&
(arr_usb_data_in[6]==0x00) &&
(arr_usb_data_in[6]==0x00)) { // .reset.. |
|
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue May 23, 2006 7:58 am |
|
|
Use memcmp with the passed in value of length. |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
|
Posted: Tue May 23, 2006 8:13 am |
|
|
What I can't figure out is how to stick the hex characters together to be used in the comparison . I cannot use the ascii equavalents becase of 03 etc |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
|
Posted: Tue May 23, 2006 8:21 am |
|
|
Can I do the following:
Code: | int8 arr_usb_data_compare[8]
arr_usb_data_compare[0] = (0x03,0x72,0x65,0x65,0x65,0x74,0x00,0x00);
if (memcmp(arr_usb_data_compare,arr_usb_data_in,8)==0) {
...
or must it be the following
if (memcmp(arr_usb_data_compare[0],arr_usb_data_in[0],8)==0) {
...
|
|
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue May 23, 2006 8:36 am |
|
|
hillcraft wrote: | ...or must it be the following
Code: | if (memcmp(arr_usb_data_compare[0],arr_usb_data_in[0],8)==0) {
... |
|
Don't do this, because it will probably produce an interesting artifact. The first members of your arrays will be treated as addresses and corresponding memory areas will be compared.
Try this instead (note the ampresands ):
Code: |
if (memcmp(&arr_usb_data_compare[0], &arr_usb_data_in[0],8)==0) {
... |
Last edited by kender on Tue May 23, 2006 9:24 am; edited 1 time in total |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
This doesn't seem to work! |
Posted: Tue May 23, 2006 9:03 am |
|
|
The input data is definitely the same
Code: | arr_usb_data_compare[0] = (0x03,0x72,0x65,0x73,0x65,0x74,0x00,0x00);
if (memcmp(&arr_usb_data_in[0],&arr_usb_data_compare[0],8)==0) { |
|
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Re: This doesn't seem to work! |
Posted: Tue May 23, 2006 9:17 am |
|
|
hillcraft wrote: | Code: | arr_usb_data_compare[0] = (0x03,0x72,0x65,0x73,0x65,0x74,0x00,0x00); |
|
It could be an error in the declaration of your constant array. Try:
Code: | int8 arr_usb_data_compare[8] = {0x03,0x72,0x65,0x73,0x65,0x74,0x00,0x00}; |
|
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
|
Posted: Tue May 23, 2006 9:27 am |
|
|
No it s not a constant array. I create a 8 element empty array that I then need to reuse throughout the program. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue May 23, 2006 9:37 am |
|
|
hillcraft wrote: | No it s not a constant array. I create a 8 element empty array that I then need to reuse throughout the program. |
Sorry, my mistake in the text, but the code (which doesn't have const) might still work. |
|
|
Ttelmah Guest
|
|
Posted: Tue May 23, 2006 9:42 am |
|
|
hillcraft wrote: | No it s not a constant array. I create a 8 element empty array that I then need to reuse throughout the program. |
int8 arr_usb_data_compare[8] = (0x03,0x72,0x65,0x73,0x65,0x74,0x00,0x00);
This creates an 8 element RAM array, and automatically initialises it with the values given.
This however wll not work:
arr_usb_data_compare[0] = (0x03,0x72,0x65,0x73,0x65,0x74,0x00,0x00);
However this:
strcpy(arr_usb_data_compare,"\x03Text2");
Will refill the whole array with the new values.
You can use hex, or octal values (\nnn, or \xnn), for anything that can't be directly typed as text.
Best Wishes |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
I'm still stumped |
Posted: Wed May 24, 2006 1:50 am |
|
|
This code:
Code: | int8 arr_usb_data_out_raw[8];
int8 arr_usb_data_out[8];
int8 arr_usb_data_in[8];
int8 arr_usb_data_compare[8];
strcpy(arr_usb_data_compare,"\x03\x72\x65\x73\x65\x74\x00\x00");
for (i=0;i<8;i++)
printf("%U: 0x%X\n\r",i,arr_usb_data_compare[i]); |
Creates this result:
Code: | 0: 0x03
1: 0x72
2: 0x65
3: 0x73
4: 0x65
5: 0x74
6: 0x00
7: 0x37 |
I cannot see where the 0x37 comes from |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 24, 2006 2:34 am |
|
|
strcpy copies up-to-and-including the first zero. In your example code the second 0x00 at the end isn't copied resulting in arr_usb_data_compare[7] to contain just any random data that was already in memory.
When dealing with data containing possible null values, use memcpy instead of strcpy. |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Sorted |
Posted: Wed May 24, 2006 7:09 am |
|
|
Thanks, I live and learn... |
|
|
|