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 to convert hexadecimal into decimal
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GG
Guest







how to convert hexadecimal into decimal
PostPosted: Thu Jul 30, 2009 9:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 30, 2009 9:58 am     Reply with quote

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







PostPosted: Thu Jul 30, 2009 10:07 am     Reply with quote

it receive serial for example FF, 01, 02. IT IS 8 Bit data
kamputty
Guest







PostPosted: Thu Jul 30, 2009 11:23 am     Reply with quote

you can use "value=atoi(buffer)"
Guest








PostPosted: Thu Jul 30, 2009 11:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 30, 2009 2:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 31, 2009 1:58 am     Reply with quote

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







PostPosted: Fri Jul 31, 2009 6:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 31, 2009 6:53 am     Reply with quote

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







PostPosted: Sat Aug 01, 2009 11:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 03, 2009 2:07 am     Reply with quote

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







PostPosted: Mon Aug 03, 2009 5:37 am     Reply with quote

is there any difference if it is a value than a hex chacracter
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 03, 2009 7:58 am     Reply with quote

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







PostPosted: Tue Aug 04, 2009 5:09 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 04, 2009 5:25 am     Reply with quote

0xFF translates to 255 in decimal.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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