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

I want extract the single digit from a int8:

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








I want extract the single digit from a int8:
PostPosted: Sat Aug 09, 2008 2:51 am     Reply with quote

Hi

I want extract the single digit from a int8:
169 I want c1=1/c2=6/c3=9

I made this func for testing. Working but not nice.

Code:
void Get_Digit(void){
//want to isolate the digit
//ex. 169: c1=9/c2=6/c3=1
     int t,c1,c2,c3,tmp;
     t=169;
     
     //any hints for optimizing
     c1=t%10;
     c2=(t-c1)/10%10;
     c3=(t-(t%100))/100;
     
     tmp= c1+c2*10+c3*100;
     printf("t:%u c1:%u c2:%u c3:%u tmp:%u",t,c1,c2,c3,tmp);
     
     
     }




Any suggestion?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Aug 09, 2008 6:15 am     Reply with quote

What you have is fine. If you must go a little faster you can subtract in a loop instead of dividing. It will save a few machine cycles but the code is harder to understand so I don't recommend it unless timing is crucial.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Indy



Joined: 16 May 2008
Posts: 24

View user's profile Send private message

PostPosted: Sat Aug 09, 2008 5:01 pm     Reply with quote

If you use an array instead of 3 separate variables you could do it like:
Code:
int8 c[3];
int8 value = 169;

for (i=3; i>0; i--)
{
  c[i-1] = value % 10;
  value = value / 10;
}
Guest








PostPosted: Sun Aug 10, 2008 2:12 am     Reply with quote

:-)
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Mon Aug 11, 2008 1:03 am     Reply with quote

If you only want to display digits, you can use sprintf.
Code:
char c[4];
sprintf(c,"%u",value);
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