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

convert int to char
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Mon Mar 29, 2010 6:06 am     Reply with quote

I don't know if I understood your question correctly, Simbo, but my suggestion is to call "kbd_getc()" three times to get three digits. Call the macro "toint(c)" on each digit and then do this calculation:

PWM = (First digit*100) + (Second digit*10) + Third digit;

Regards,
Adriano.
Simbo



Joined: 02 Mar 2008
Posts: 7
Location: UK

View user's profile Send private message MSN Messenger

PostPosted: Wed Mar 31, 2010 2:34 pm     Reply with quote

hi,

Sorry I was a bit vague with my question but I think you get the jist of it!

I think your suggestion should do it.
Quote:

PWM = (First digit*100) + (Second digit*10) + Third digit;

I'll adapt the code and try it again soon.
Thank you for the reply.

Simbo
_________________
<~Simbo~>
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Mar 31, 2010 3:37 pm     Reply with quote

Can I suggest a fractionally different approach.
Assuming characters are available, from a buffer, or using getc, something like:
Code:

int32 total=0; //start with total=0
int8 chr;
while (isdigit(chr=getc()) { //Get a character - is it a digit?.
   total=mult_with10(total)+(chr-'0'); //if so, multiply total*10,
                                                     //and add the digit value
}

//total now contains the value typed in....



Multiple key things:
stdlib.h, has a very efficient *10, supporting up to an int32.
All you do, is multiply the total 'so far' by ten, and add in the value of the new digit, stopping when something that is not a digit arrives.
Obviously you can use a smaller type (int8, int16 etc.), but as shown this will handle integers of any normal size, quickly and efficiently.

Best Wishes
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 Previous  1, 2
Page 2 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