View previous topic :: View next topic |
Author |
Message |
GG Guest
|
how to convert hexadecimal into decimal |
Posted: Thu Jul 30, 2009 9:52 am |
|
|
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. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 30, 2009 9:58 am |
|
|
How is it received ? Serial ?
What is the format ?
What is the separator ?
Is it 8 bit, 16 bit, 32,64, n bit ?
Code: | e.g. "00AABB"
"0x00"
"0x0000"
"AA,BB,CC"
"0x00, 0x01" |
An example of the data you want to convert would be useful. |
|
|
GG Guest
|
|
Posted: Thu Jul 30, 2009 10:07 am |
|
|
it receive serial for example FF, 01, 02. IT IS 8 Bit data |
|
|
kamputty Guest
|
|
Posted: Thu Jul 30, 2009 11:23 am |
|
|
you can use "value=atoi(buffer)" |
|
|
Guest
|
|
Posted: Thu Jul 30, 2009 11:25 am |
|
|
kamputty wrote: | you can use "value=atoi(buffer)" |
I'm assuming the "buffer" is a string that you received (ie. "F", "8", "FF") |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 30, 2009 2:16 pm |
|
|
How are you getting the data ? Is it coming from a keypad ?
There is a gethex() routine in this file:
Quote: | c:\program files\picc\drivers\input.c |
It receives two ASCII hex characters and converts them to an 8-bit
integer. You can get three 8-bit integers, and then combine and put
them into a 32-bit variable by using the make32() function. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jul 31, 2009 1:58 am |
|
|
atoi requires 0x before the hex value otherwise it will convert 56(hex) into 56 decimal. e.g. "0x56" = 86 Decimal
Now the only issue left is does your input string (char array) contain 1 value or multiple ?
e.g. 1 value = "FF"
multiple = "FF, 01, 02"
If it is the first then you can do 1 of 2 things.
1. Preappened "0x" to the start of the string and use atoi
2. Write your own routine to convert the 2 chars to a value.
Code: |
char buf[3] = "F1";
int a;
if (buf[0] > '9')
a = toupper(buf[0]) - 'A' + 10;
else
a = buf[0] - '0';
a <<= 4;
if (buf[1] > '9')
a += (toupper(buf[1]) - 'A' + 10);
else
a += (buf[1] - '0');
|
NOTE: I have not checked this, but I am sure it is ok.
Or something similar, Now if you know for definate that all chars are upper or lower case then you can remove the toupper and adjust the 'A' acordingly.
Now for multiple values you do the same but you have to traverse the string (parse) and retrieve the values to convert them. |
|
|
GG Guest
|
|
Posted: Fri Jul 31, 2009 6:13 am |
|
|
Code: | for(i=1;i<6;i++)
{
keyin_C[i]=fgetc(COM_C);
}
x2 =keyin_C[4]; |
This is my code. after I executed, the value in the x2 will a byte for example 01,02,FE, FF. Then how can I use as you suggested "Preappened "0x" to the start of the string and use atoi"
x2 is declared as int8 |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jul 31, 2009 6:53 am |
|
|
Actually your program will not work.
Code: |
for(i=1;i<6;i++)
{
keyin_C[i]=fgetc(COM_C);
}
x2 =keyin_C[4];
|
x2 will contain the value (byte) at keyin_C[4]
if keyin_C = "00, FF, 01, CC"
then x2 will = 'F' or decimal 70 (hex) 0x46
You would be better off using a routine to retrieve the hex value rather than atoi but if you wanted to use atoi you could do
Code: |
char hex[5] = "0x00";
int x2;
hex[2] = keyin_C[4];
hex[3] = keyin_C[5];
x2 = atoi(hex);
|
|
|
|
GG Guest
|
|
Posted: Sat Aug 01, 2009 11:50 am |
|
|
I already tried using atoi but it doesn't work. It always displays 0. I don't why it is not working. Using my code when I display the value in x2 it is 01 but when using atoi it displays 0. This input comes from an optical sensor.
Is there any way to calculate using hexadecimal? Because sometimes the angle is too small, for example 0.8 degree. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Aug 03, 2009 2:07 am |
|
|
if you are displaying x2 with printf("%x", x2); and it is showing 01 then the value at keyin_C[4] is not a hex char '0' - 'F' but contains a value of 1.
Are you sure you are reading in hex chars ? and not just values! |
|
|
GG Guest
|
|
Posted: Mon Aug 03, 2009 5:37 am |
|
|
is there any difference if it is a value than a hex chacracter |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Aug 03, 2009 7:58 am |
|
|
Yes.
You stated before that you are expecting the following input
"FF,01,02" Without spaces
if this was the case and you did recieve this then the value in
keyin_C[4] would be the character '1' so
x2 = keyin_C[4]; x2 would = 49 (Decimal) or 0x31 (Hex) '1' (Ascii)
You would need to convert this to a value. 1 in the case.
If the data you get is the numerical value of what you have stated then it would be
keyin_C[0] = 15 (F)
keyin_C[1] = 15 (F)
keyin_C[2] = 44 (,)
keyin_C[3] = 0 (0)
keyin_C[4] = 1 (1)
but then I would have expected the data to be
keyin_C[0] = 255 (0xFF)
keyin_C[1] = 1 (0x01)
keyin_C[2] = 2 (0x02)
the ',' would not be needed.
So you need to confirm what data you are recieving or expecting.
What are the values in keyin_C ?
What is sending them to the PIC ? |
|
|
GG Guest
|
|
Posted: Tue Aug 04, 2009 5:09 am |
|
|
the input comes from optical mouse. each time the mouse will send 3 packet of data (3 bytes) which corresponding to button ,x and y position. which I used a FOR loop to collect the data. for example
for(i=1;i<6;i++)
{
keyin_C[i]=fgetc(COM_C);
}
button = keyin_C[4];
x =keyin_C[4];
y =keyin_C[5];
*keyin_C[1] and keyin_C[2] will not be used
each of them will have a packet. for example button = 08, x=01, y=ff; what I need is to convert the value in x any y into decimal so that I can form a calculation such as division to find the angles |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
|
Posted: Tue Aug 04, 2009 5:25 am |
|
|
0xFF translates to 255 in decimal. |
|
|
|