View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
How store values hexdecimal in a variable |
Posted: Fri Nov 26, 2010 10:26 am |
|
|
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
|
|
Posted: Fri Nov 26, 2010 10:43 am |
|
|
Don't...
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
|
|
Posted: Fri Nov 26, 2010 11:03 am |
|
|
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
|
|
Posted: Fri Nov 26, 2010 4:41 pm |
|
|
Thank you.. |
|
|
|