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

How to split a int16 in separate "hundred"

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







How to split a int16 in separate "hundred"
PostPosted: Fri Jul 14, 2006 7:39 am     Reply with quote

Hi all,

I have a long integer number for example 1015 from the adc.
Now I want to get:

thousand: 1
hundred: 0
ten: 1
one: 5

int or char - but how?
sjbaxter



Joined: 26 Jan 2006
Posts: 141
Location: Cheshire, UK

View user's profile Send private message Visit poster's website

PostPosted: Fri Jul 14, 2006 7:51 am     Reply with quote

You could try something like:

Code:

int16 adcvalue;
int8 thous,huns,tens,units;

adcvalue = 1015;

units = (int8)(adcvalue % 10);
tens = (int8)((adcvalue / 10) % 10);
huns = (int8)((adcvalue / 100) % 10);
thous = (int8)((adcvalue / 1000) % 10);


(have read about the MODULUS operator !!)

or if you are going to output the value as ascii text, have a read about the use of printf.
_________________
Regards,
Simon.
Wolfi
Guest







PostPosted: Fri Jul 14, 2006 10:43 am     Reply with quote

Hi sjbaxter,
o.k. this will work, but much time to calculate... what about printf, can I simple use the int 16 into a char? And do you have a instruction in mind?
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 14, 2006 12:30 pm     Reply with quote

With printf (or sprintf) you could use a format specifier string (consult your manual). Then your digits will always fall into the same index of the array or the same location on the screen.

%04d for example.

But depending on how things work out, this might actually take more instruction cycles than using integer division and the modulus operator. The example given above could probably be optimized for higher execution speed.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Wolfi
Guest







PostPosted: Fri Jul 14, 2006 12:49 pm     Reply with quote

thous = (int8)((adcvalue / 1000) % 10);

I dontīt understand the: %10 ... what does it?

Also 1015/100 = 10 - the huns are 0...?
Wolfi
Guest







PostPosted: Fri Jul 14, 2006 1:00 pm     Reply with quote

O.k. I found out what %10 means, but this does not help for my problem. I donīt need 10 as result for hundred, I need a 0, when I take my example 1015
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 14, 2006 1:06 pm     Reply with quote

Code:

int adcvalue = 1015;
int thou, huns, tens, ones;
char temp[8];

// untested and unoptomized, use at your own peril
thou = adcvalue / 1000;
adcvalue %= 1000;
huns = adcvalue / 100;
adcvalue %= 100;
tens = adcvalue / 10;
adcvalue %= 10;
ones = adcvalue;

// and
sprintf(temp,"%04d", adcvalue);
putch(temp[0]);
putch(temp[1]);
putch(temp[2]);
putch(temp[3]);


_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!


Last edited by rwyoung on Fri Jul 14, 2006 1:08 pm; edited 1 time in total
sjbaxter



Joined: 26 Jan 2006
Posts: 141
Location: Cheshire, UK

View user's profile Send private message Visit poster's website

PostPosted: Fri Jul 14, 2006 1:07 pm     Reply with quote

This must be a bug in the CCS modulus operator, or you haven't actually divided by 100, as the code I provided works in a few of my applications both on the PIC and in windows c/c#/c++ code !
_________________
Regards,
Simon.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jul 14, 2006 5:12 pm     Reply with quote

And because there is always another way to do it...
The optimised version below is more source code but actually compiles about 130 bytes shorter and is much faster to execute.

Code:
void GetDigits(int16 Val, int8 &thous, int8 &huns, int8 &tens, int8 &units)
{
  tens = 0;
  huns = 0;
  thous = 0;
 
  while (Val >= 1000)
  {
    Val -= 1000;
    thous++;
  }
 
  while (Val >= 100)
  {
    Val -= 100;
    huns++;
  }
 
  while (Val >= 10)
  {
    Val -= 10;
    tens++;
  }

  units = Val;
}

//===============================
void main ()
{
  int16 adcvalue;
  int8 thous,huns,tens,units;

  adcvalue = 1015;

  GetDigits(adcValue, thous, huns, tens, units);
 
  while (1);
}


Edit: Fixed a bug in the end condition of the while loops. Thanks JohnP.


Last edited by ckielstra on Sun Jul 16, 2006 7:10 am; edited 1 time in total
Wolfi
Guest







PostPosted: Sat Jul 15, 2006 3:19 am     Reply with quote

Perfect... thats what Iīm looking for. I had now something like this, but not in this short form!
Thanks!
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Sat Jul 15, 2006 7:38 pm     Reply with quote

Uh, not so fast with the thanks there. Shouldn't all those

while (Val > 1000)

etc lines there be

while (Val >= 1000) ?

If you use the code as listed and the incoming value were (let's say) exactly 1000, the first test would never indicate any thousands, but would go on to the next stage with val still equal to 1000. Then it would count down 9 hundreds and go on to the final stage with 100 left, then count 9 tens and finally there'd be 10 units. Not good.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 16, 2006 7:07 am     Reply with quote

John P wrote:
Uh, not so fast with the thanks there. Shouldn't all those

while (Val > 1000)

etc lines there be

while (Val >= 1000) ?
Oops... Embarassed
Thanks for pointing out the error, I've edited the sample program to fix this.
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