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

Decimal to digit ???

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



Joined: 06 Dec 2006
Posts: 102
Location: Montreal , Canada

View user's profile Send private message

Decimal to digit ???
PostPosted: Wed May 30, 2007 5:13 am     Reply with quote

Hi guys ,
is there an easy one to separate digit ?? let say i have

Z=6234 (base10) ,

i would like to have it as

digit1 = 6, digit2 = 2, digit3 = 3, digit4 = 4

i know it should be easy , but my brain is gone for vacation Embarassed

Thanks
Pret



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

PostPosted: Wed May 30, 2007 6:12 am     Reply with quote

You can use itoa... or sprintf(str, "%lu", val)

Or you cand create you own code.... using %10 and /10 repeatedly... then after reverse the result. I recomand sprintf.
mindstorm88



Joined: 06 Dec 2006
Posts: 102
Location: Montreal , Canada

View user's profile Send private message

PostPosted: Wed May 30, 2007 7:14 am     Reply with quote

Anybody sees something wrong in it ???
Code:


int digit[4] = {0,0,0,0};

count=6384;
digit[0]=count/1000;                                          // 1000
digit[1]=( count - ( digit[0] * 1000 ) ) / 100;                        // 100
digit[2]=( count - ( ( digit[0] * 1000 ) + ( digit[1] * 100 )))/10;            // 10
digit[3]=count - (( digit[0] * 1000 ) + ( digit[1] * 100 ) + (digit[2] * 10));   // 1
digit[0] and [1] are ok but [2] and [3] are bad !! i get

6
3
34
0

version 4.023
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Wed May 30, 2007 10:15 am     Reply with quote

Hello,

It's a castup probleme.

Code:

  digit[0]=count/1000;                                          // 1000
  digit[1]=( count - ( (long)digit[0] * 1000 ) ) / 100;                        // 100
  digit[2]=( count - ( ( (long)digit[0] * 1000 ) + ( (long)digit[1] * 100 )))/10;            // 10
  digit[3]=count - (( (long)digit[0] * 1000 ) + ( (long)digit[1] * 100 ) + ( (long)digit[2] * 10));   // 1


So it work.

dro.
_________________
in médio virtus
mindstorm88



Joined: 06 Dec 2006
Posts: 102
Location: Montreal , Canada

View user's profile Send private message

PostPosted: Wed May 30, 2007 10:28 am     Reply with quote

Smile Thanks that was it !! , totally forgot that "long" was needed for the calculation!!
roykyn



Joined: 25 Nov 2006
Posts: 9

View user's profile Send private message

hi
PostPosted: Wed Jun 13, 2007 1:42 pm     Reply with quote

i have an idea...use a for loop it will decrease the code memory a lot like this...but this method too consumes a lot of memory according to me...
i am trying to find shorter and faster code.......i also found out it consumes less memory by transferring it to j(int) and then to digit[i]. hope this will help you


Code:
int i,j;
long A,e;
A=6384;
for(i=0;i<4;i++)
      {
         e = A / 10;
         j= A-(10*e);
                        digit[i]=j;
         A=e;

      }
Laughing Laughing Laughing Laughing Laughing Laughing Laughing
Pret



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

PostPosted: Thu Jun 14, 2007 1:42 am     Reply with quote

Pret wrote:
You can use itoa... or sprintf(str, "%lu", val)

Or you cand create you own code.... using %10 and /10 repeatedly... then after reverse the result. I recomand sprintf.


Take my advice again. This is the basic algorithm for converting int to string.
Code:
int8 i=0; int16 n;
....
while(n)
{
  digit[i] = n % 10;
  n = n / 10;
  i++;
}
then reverse it if you need to.
or:
Code:
 sprintf(digit, "%lu", n);
mindstorm88



Joined: 06 Dec 2006
Posts: 102
Location: Montreal , Canada

View user's profile Send private message

PostPosted: Thu Jun 14, 2007 6:34 am     Reply with quote

Thanks Pret , your basic algoritm :
Code:

int8 i=0; int16 n;
....
while(n)
{
  digit[i] = n % 10;
  n = n / 10;
  i++;
}


is saving me 125 ROMs spaces compare to mine
Code:

digit[0]=count/1000;                                          // 1000
  digit[1]=( count - ( (long)digit[0] * 1000 ) ) / 100;                        // 100
  digit[2]=( count - ( ( (long)digit[0] * 1000 ) + ( (long)digit[1] * 100 )))/10;            // 10
  digit[3]=count - (( (long)digit[0] * 1000 ) + ( (long)digit[1] * 100 ) + ( (long)digit[2] * 10));   // 1
 



Someday i'll be a good coder too , working on it !!!!! Very Happy
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