CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Decompicating an array

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

Decompicating an array
PostPosted: Tue May 23, 2006 7:48 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue May 23, 2006 7:58 am     Reply with quote

Use memcmp with the passed in value of length.
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue May 23, 2006 8:13 am     Reply with quote

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)

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue May 23, 2006 8:21 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue May 23, 2006 8:36 am     Reply with quote

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. Shocked

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)

View user's profile Send private message Send e-mail Visit poster's website

This doesn't seem to work!
PostPosted: Tue May 23, 2006 9:03 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Re: This doesn't seem to work!
PostPosted: Tue May 23, 2006 9:17 am     Reply with quote

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)

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue May 23, 2006 9:27 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue May 23, 2006 9:37 am     Reply with quote

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







PostPosted: Tue May 23, 2006 9:42 am     Reply with quote

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)

View user's profile Send private message Send e-mail Visit poster's website

I'm still stumped
PostPosted: Wed May 24, 2006 1:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 24, 2006 2:34 am     Reply with quote

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)

View user's profile Send private message Send e-mail Visit poster's website

Sorted
PostPosted: Wed May 24, 2006 7:09 am     Reply with quote

Thanks, I live and learn...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group