View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
how to convert char to int8? |
Posted: Fri Nov 05, 2004 1:51 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 2:09 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 2:13 pm |
|
|
and how them recevie the return. like the difference of printf("RB") and printf("RB\r"); |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Fri Nov 05, 2004 2:22 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 2:28 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 2:38 pm |
|
|
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 |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Fri Nov 05, 2004 2:48 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 3:01 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 3:12 pm |
|
|
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 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Nov 05, 2004 5:01 pm |
|
|
lookup
atoi()
atol() |
|
|
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
|
|
|