| View previous topic :: View next topic |
| Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Tue Aug 04, 2009 5:43 am |
|
|
mabey im mistaken, but i was under the impresion you could do that with using the output formating options... something like:
Printf(%c\d, Hexnum); //Hexnum being your variable holding the number
don“t recall the exact format, but if you search the printf() function on the help/contents part of the compiler... they give you a list of possible output formats... _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
GG Guest
|
|
Posted: Tue Aug 04, 2009 6:01 am |
|
|
arunb, what I mean is in programming.
gabriel, I already tested changing the output formating. I'm using %LX |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 04, 2009 6:18 am |
|
|
| arunb wrote: | | 0xFF translates to 255 in decimal. |
Who are you correcting ?
| Gabriel wrote: | | Printf(%c\d, Hexnum); |
This will output the ascii char of the value held in Hexnum. %d will output the decimal number held in Hexnum, %x will output the hex value of Hexnum.!!!!!!!!!!
GG, the values you read in are numbers, stored in the pic as binary values. They are not hex, they are not decimal, thay are not ascii representations of the values you are expecting. They are the values you are expecting. I am finding this very hard to explain to you. You do not need to convert them!
x = keyin_C[4]; // x = 01 as an example
As this is the actual value of the mouse postition x = 1 = the mouse x postition!
If you want to do math on it:-
x = x * 10;
x = x / 10;
The same goes for y;
button = keyin_C[4]; // <----- this a mistake as it is the same as x!
Anyway, button = 08 = 0b00001000, prob means that button D is pressed!
As for calculating the angles
oldx = ?
oldy = ?
x = ?
y = ?
You first need to work out which calculation you need to use
e.g.
angle = atan((x - oldx) / (y - oldy));
Or something like that.
Also, does the mouse only have movement of 0 to 255 points ? |
|
 |
Guest
|
|
Posted: Tue Aug 04, 2009 6:33 am |
|
|
WAYNE
| Quote: | | %d will output the decimal number held in Hexnum |
isent that what he wanted?
to get the decimal value of a Hex number?
like i said, i didnt remember the correct format... but GG got my point, which basically was: "try ouput formatting". |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Tue Aug 04, 2009 6:35 am |
|
|
that "guest" comment was me.... forgot to log in.... sorry _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
| Re: how to convert hexadecimal into decimal |
Posted: Tue Aug 04, 2009 6:50 am |
|
|
Gabriel, read GGs first post.
| GG wrote: | | I'm working on a project that receives hexadecimal as its input. I need to convert this input into decimal so that I can form the calculation to find an angle. |
He stated that he wanted to convert a hex value to decimal. This would imply that he has read a hex value in (as ascii) "00, FF, 01" note the " to represent a STRING. This then would need to be converted to the correct values 0, 255, 1 in order to process the values.
Now as it happens he is not recieving HEX values (as chars) but is actually reading in the value as an int.
BUT, even if he were wanting to convert a "HEX" value to a decimal printf would not work.
Take a value
int Hexnum = 0xFF;
In reality Hexnum is stored as 0b11111111 which is the same as 0xFF which is also the same as 255 (Decimal)
so Hexnum does NOT need to be converted.
You can do decimal math
int myval = Hexnum * 10;
Hex math
int myval = Hexnum * 0x0A;
Char math
int myval = Hexnum - 'A';
BUT if you have
char[3] Hexnum = "FF";
Then this needs to be converted and as stated atoi requires it to be prefixed with 0x so
char[5] Hexnum = "0xFF";
int myVal = atoi(Hexnum); would return the value 255, 0xFF |
|
 |
GG Guest
|
|
Posted: Tue Aug 04, 2009 7:16 am |
|
|
the mouse will has +-255 count. wayne, do you mean that I can form the calculation without converting. let say I have
x1 = 08
x2 = 0b
length = bb or should I used 187 instead
the calculation should be (x2-x1)/length
hand calculation the answer is 0.016
then in programming how this can be represent |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Tue Aug 04, 2009 7:17 am |
|
|
| Quote: | | This would imply that he has read a hex value in (as ascii) "00, FF, 01" note the " to represent a STRING. |
HA! i didnt see that part.... yeah printf wouldnt work for that
off topic* ..hey by the way thanks for your help on my GSM post (wayne) _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 04, 2009 7:18 am |
|
|
| Gabriel wrote: |
off topic* ..hey by the way thanks for your help on my GSM post (wayne) |
No prob, hope it worked | |
|
 |
GG Guest
|
|
Posted: Tue Aug 04, 2009 9:51 am |
|
|
float angle;
int16 x1,x2;
int16 length;
int16 minus
x1=01;
x2=04;
minus = x2-x1;
length = 187;
angle = (float)minus / (float)length;
can it be done like this |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 04, 2009 10:01 am |
|
|
Yes GG but you have to be careful
| Quote: |
x1 = 08
x2 = 0b
length = bb or should I used 187 instead
the calculation should be (x2-x1)/length
hand calculation the answer is 0.016
|
0xbb or 187 doesn't matter at all they are the same.
can x2-x1 be negative ?
You either check for this and reverse the calculation
Also the equation (x2 - x1)/length will only use int math because there are no floats in the equation so you need to cast.
| Code: |
float angle;
int x1 = 8;
int y1 = 0x0b;
int length = 0xbb; // or = 187
if (x2 > x1)
angle = (float)(x2 - x1) / length;
else
angle = (float)(x1 - x2) / length;
|
That should do it. |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 04, 2009 10:08 am |
|
|
It is unlikely to be +-255 as this would require 16 bits (well 9 at least) so it is either
-128 to +128 (signed int) or
0 to 255 (unsigned int)
int x1 in CCS is unsigned by default so x1 can only be 0 to 255. |
|
 |
GG Guest
|
|
Posted: Thu Aug 06, 2009 8:28 am |
|
|
| It works. Thanks to all |
|
 |
|