|
|
View previous topic :: View next topic |
Author |
Message |
Lukas89
Joined: 06 May 2012 Posts: 2
|
converting integer to character |
Posted: Sun May 06, 2012 3:20 pm |
|
|
Hello,
I'm using PIC18F4550 and hyperterminal communication via USB. I want to convert integer value to character, it's for displaying in LabView graph. Code I'm using is below:
Code: |
void main(){
int a=25;
char str [33],RcvUSB;
usb_cdc_init();
usb_init();
do {
usb_task();
if (usb_enumerated()) {
RcvUSB=usb_cdc_getc();
if(RcvUSB='q'){
printf(usb_cdc_putc,"\n\r%c", RcvUSB);
itoa(a,str,10);
printf(usb_cdc_putc,"\n\rdecimal = %s",str);
itoa(a,str,2);
printf(usb_cdc_putc,"\n\rbinary = %s",str);
itoa(a,str,16);
printf(usb_cdc_putc,"\n\rhexademical = %s",str);
}while(TRUE)
} |
OUTPUT(copy from terminal):
q
decimal =
binary =
hexademical =
I got unreadable character, in fact somehow its looks like smileface. I tried to use %c, %d instead of %s, but nothing seemed working. I will appreciate for your attention. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Sun May 06, 2012 4:21 pm |
|
|
you need to look at itoa in the compiler manual again. Specifically the parameter order. |
|
|
Lukas89
Joined: 06 May 2012 Posts: 2
|
|
Posted: Sun May 06, 2012 4:38 pm |
|
|
Indeed, the order was wrong. Looked at itoa function and changed code:
Code: |
void main(){
char RcvUSB;
usb_cdc_init();
usb_init();
do {
usb_task();
if (usb_enumerated()) {
RcvUSB=usb_cdc_getc();
if(RcvUSB='q'){
int32 a=25;
char str [33];
printf(usb_cdc_putc,"\n\r%c", RcvUSB);
itoa(a,10,str);
printf(usb_cdc_putc,"\n\rdecimal = %s",str);
itoa(a,2,str);
printf(usb_cdc_putc,"\n\rbinary = %s",str);
itoa(a,16,str);
printf(usb_cdc_putc,"\n\rhexademical = %s",str);
}while(TRUE)
} |
OUTPUT(copy from terminal):
q
decimal = 25
binary = 11001
hexademical = 19
Thanks jeremiah. |
|
|
|
|
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
|