View previous topic :: View next topic |
Author |
Message |
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
Decimal to digit ??? |
Posted: Wed May 30, 2007 5:13 am |
|
|
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
Thanks |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Wed May 30, 2007 6:12 am |
|
|
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
|
|
Posted: Wed May 30, 2007 7:14 am |
|
|
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
|
|
Posted: Wed May 30, 2007 10:15 am |
|
|
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
|
|
Posted: Wed May 30, 2007 10:28 am |
|
|
Thanks that was it !! , totally forgot that "long" was needed for the calculation!! |
|
|
roykyn
Joined: 25 Nov 2006 Posts: 9
|
hi |
Posted: Wed Jun 13, 2007 1:42 pm |
|
|
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;
} | |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Thu Jun 14, 2007 1:42 am |
|
|
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
|
|
Posted: Thu Jun 14, 2007 6:34 am |
|
|
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 !!!!! |
|
|
|