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

Modulus Inquiry

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



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

Modulus Inquiry
PostPosted: Thu Jul 29, 2004 5:08 pm     Reply with quote

Hi

I have a 16bit integer say... 2356 and I want to store it as a 4 8bit variable.. e.g.
var1 = 2;
var2 = 3;
var3 = 5;
var6 = 6;

I understand using modulus would help... anyone have any great idea in handling this in the most effecient way...

A code snippet will help...

thnx
Guest








PostPosted: Thu Jul 29, 2004 11:09 pm     Reply with quote

It can be done with modulus, but sprintf is easier.
Example:


Code:
int8 array[6];
int8 i;
int16 num = 2356;
 
void main()
{

   // With sprintf
   sprintf(array,"%lu",num);

   for (i=0;i<4;i++)
      putc(array[i]);
   putc('\n');
   putc('\r');   
   
   
   // With modulus
   while (i <= 4) {
     if (i != 0) num /= 10;   // Don't need to divide to get units digit
     array[i] = num % 10;
     i++;
   }

   for (i=0;i<5;i++)
      putc(array[i]);
     
   putc('\n');
   putc('\r');   
     
   while(1);   
   

}
Guest








PostPosted: Fri Jul 30, 2004 12:38 am     Reply with quote

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