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

incrementing a hex number

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







incrementing a hex number
PostPosted: Sat Feb 22, 2003 7:32 pm     Reply with quote

I'm trying to incrment BCD number, what's the best way to do this?
___________________________
This message was ported from CCS's old forum
Original Post ID: 12025
Pete Smith
Guest







Re: incrementing a hex number
PostPosted: Sun Feb 23, 2003 3:16 am     Reply with quote

:=I'm trying to incrment BCD number, what's the best way to do this?

If you're doing a lot with BCD numbers, it would be useful to have a hex2bcd and bcd2hex routine. To increment the number, you'd just convert the number back into hex/decimal, increment it, and then convert it back again.

Here's what I use...

This one takes a BCD number, and returns the decimal equivalent, ie passing it 10 (BCD, real value=13) will return 10.

int bcd(int input)
{
int temp,temp2;

temp=(input & 0x0f);
temp2=((input & 0xf0)/16);

temp+=temp2*10;

return(temp);
}

This one does the opposite. If you pass it 10 (decimal), it'll return 10 BCD (13).

int to_bcd(int input)
{
int temp,temp2;

temp=input \% 10;
temp2=input/ 10;

temp2=temp2*16;

temp2+=temp;

return(temp2);
}

HTH

Pete.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12030
R.J.Hamlett
Guest







Re: incrementing a hex number
PostPosted: Sun Feb 23, 2003 4:38 am     Reply with quote

:=I'm trying to incrment BCD number, what's the best way to do this?

A lot depends on whether you are after the smallest code space, or the fastest speed. You can either code using the 'modulus' function (\%), which gives simple code, but can be fairly slow, or just do a normal increment, and then adapt if there was a wrap over (this is the approach used in some microprocessors). The same applies when converting to/from BCD, where the use of a look up table, can be quicker, but will be bulkier than simple arithmetic.
The code below is a standard set of routines using the latter approach (including the conversions). Someone else has allready posted code using the former approach.

const unsigned int conv[16] = {
0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,90 };
#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])

/* Generic routine to increment a BCD number stored in an unsigned INT */
int BCDINC(int val)
{
/* routine to increment a BCD number */
if ((val & 0xf)>8)
{
/* here there must be a wrap */
val=val+7;
}
else
val++;
return(val
}

/* BCD conversion sub */
unsigned int toBCD(unsigned int val)
{
int ctr;
for (ctr=0;ctr<10;ctr++)
{
if (val == conv[ctr])
return(ctr<<4);
else if (val < conv[ctr]) break;
}
--ctr;
return((val-conv[ctr])+(ctr<<4));
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 12032
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