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

How store values hexdecimal in a variable

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

How store values hexdecimal in a variable
PostPosted: Fri Nov 26, 2010 10:26 am     Reply with quote

Hi, I need to store the following values hexdecimal in a variable , someone could tell me how?

These are the Values:

Code:
 18 16 af 16 02 10 01 10 22 14 55 01 20 07 00 60
 02 00 30 00 40 88 67 65 f4 6f 8e 69 63 61 20 05


I need to do this and then compare these data with other data hexadeximales that I will receive through the UART
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Nov 26, 2010 10:43 am     Reply with quote

Don't... Smile
The chip works in binary. Hexadecimal, is just a way of representing numbers.
You can put values into an array in any number format you want, but they'll be stored as binary internally. Constant declarations support binary, decimal, octal, and hex input.
Then simply convert your incoming text to binary as well, and compare.

You can put constant values into an array, with a mix of formats. So:
Code:

int8 test[] = {123,0x23,017,0b11000};


That stores '123' (input as a decimal - binary 01111011), followed by 00100011 (input as hex), followed by 000010111 (input as octal!), and finally 00011000 (input in binary). The compiler is happy to convert any of these formats into binary for it's internal use.
In your case just use hex...

Then, look in 'input.c', and you will find a routine called 'gethex', that reads two characters from getc, and converts these to a byte, suitable for direct comparison with the above stored numbers.

Much easier than trying to store and compare _text_.

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Fri Nov 26, 2010 11:03 am     Reply with quote

Your question probably needs to be more specific.
Hexadecimal is notation for integers in base 16. To the pic everything is internally notated in binary. Now humans like alphabets so we use characters 0..9 abcdef to notate hexadecimal. Suppose the transmission to your uart uses this alphabetic notation and that the characters are themselves encoded using ascii then you have choices to make.
You could store the values internally in the PIC into a character array in which two characters store the ascii value of the alphabetic hexadecimal notation. Ex the character 3 ascii (51 in base 10). This choice involves always checking 2 char for comparisons. Alternatively you could convert the two ascii chars as follows and renotate the value as int 8.
int8 high nibble=(ascii val High nibble char-48) when 0...9
and (ascii val High nibble char-96)+9 when a....f
int8 low nibble=(ascii val low nibble char-48) when 0...9
and (ascii val low nibble char-96)+9 when a....f

int8 test var=(high bibble)*16+(low nibble)
Now if you define your constant hexadecimal table to the compiler using an array of int8 and syntax 0x00.......0x0f the compiler will notate this array internally as binary.
CCS has examples of hex conversion with functions like get_hex that will perform the pseudo code above.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Fri Nov 26, 2010 4:41 pm     Reply with quote

Thank you..
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