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 char to int8?

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



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

how to convert char to int8?
PostPosted: Fri Nov 05, 2004 1:51 pm     Reply with quote

I am using RS232 to transfer a integer to a PIC, How can I transfer it from char to int8 after I get the data in the PIC side from rs232?
valemike
Guest







PostPosted: Fri Nov 05, 2004 2:09 pm     Reply with quote

int8 and char and int
are both 8 bits long. So I don't think there is any need for conversion nor typecasting.

e.g.

int8 x;
char y;

y = '0'';
x = y; // x is actually 0x30 now (ascii '0'), but you can still use it.
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 2:13 pm     Reply with quote

and how them recevie the return. like the difference of printf("RB") and printf("RB\r");
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 2:22 pm     Reply with quote

Code:
 b=getchar();
  b1=getchar();
  b2=getchar();
 
 
  tilt=(int8)b2;
 
 
 if(tilt==5)
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}

/* if(b2=='5')
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}
*/



but you see when I tried b2=='5' led blinked, when I use b3==5 it did not. why?
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 2:28 pm     Reply with quote

sorry some typo

Code:

b=getchar();
  b1=getchar();
  b2=getchar();
 
 
  tilt=(int8)b2;
 
 
 if(tilt==5)
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}

/* if(b2=='5')
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}
*/

this part works
Code:

 if(b2=='5')
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}   


this part does not work
Code:

if(tilt==5)
  {output_high(PIN_A2);
  delay_ms(300);
  output_low(PIN_A2);
  delay_ms(300);}

please help
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 2:38 pm     Reply with quote

When you write '5' you mean the number 5 in ASCII wich is = 0x35
When you write 5 you mean the number 5 in decimal wich is = 0x05

Quote:

b=getchar();
b1=getchar();
b2=getchar();


To be sure what you get with getchar() just

printf(" %X %X %X ", b, b1, b2); // In HEXADECIMAL

printf(" %d %d %d ", b, b1, b2); // In DECIMAL


HTH

Humberto Very Happy
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 2:48 pm     Reply with quote

I could not check it , because, I am using the comm port already. but I believe that your are right, that is why I am think to convert ascII '5' to decimal 5.

do you know how?
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 3:01 pm     Reply with quote

I used the following method:

tilt=b2-30

how about I transfer a big number like 65100 from RS232 to PIC, How I can I transfer it to decimal?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Nov 05, 2004 3:12 pm     Reply with quote

Quote:

I used the following method:

tilt=b2-30


tilt = B2 - '0' ; // is better, has a meaning.

Please get an ASCII table, print it and read it. Also I suggest some reading
regarding different numeric systems.

Humberto Very Happy
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Fri Nov 05, 2004 5:01 pm     Reply with quote

lookup

atoi()
atol()
rrb011270



Joined: 07 Sep 2003
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Nov 06, 2004 9:50 am     Reply with quote

Try visit this link from this forum... maybe this can help.

http://www.ccsinfo.com/forum/viewtopic.php?p=33964#33964
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