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 support@ccsinfo.com

converting integer to character

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



Joined: 06 May 2012
Posts: 2

View user's profile Send private message

converting integer to character
PostPosted: Sun May 06, 2012 3:20 pm     Reply with quote

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: 1328

View user's profile Send private message

PostPosted: Sun May 06, 2012 4:21 pm     Reply with quote

you need to look at itoa in the compiler manual again. Specifically the parameter order.
Lukas89



Joined: 06 May 2012
Posts: 2

View user's profile Send private message

PostPosted: Sun May 06, 2012 4:38 pm     Reply with quote

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.
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