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

bcd Conversion of 16bit number

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



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

bcd Conversion of 16bit number
PostPosted: Wed Feb 01, 2006 4:17 am     Reply with quote

Hi Everibody,

I need to convert 16 bit hex number into BCD 5 digit.

Is there any function in CCS compiler to do it ?

Thanks for support,

Regards,
Ttelmah
Guest







PostPosted: Wed Feb 01, 2006 5:00 am     Reply with quote

A 16bit number, will only correspond to 4BCD characters, not five. The 'point' about BCD, is that it uses four bits to represent a single digit.
There have been library routines published here in the past (I have posted some, and have seen others). The DS1302.c driver in the standard library, also contains such a routine. These handle single bytes, but a call to 'make8', will allow a 16bitvalue to be handled as well.

Best Wishes
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Wed Feb 01, 2006 6:46 am     Reply with quote

Hi,


I need to convert an 16 bit hex number , for example 0xffff, to decimal 5 digit as 6 5 5 3 5.

In past I wrote an assembler routine for motorola HC11. Have you got any suggestion to do that with CCS compiler ?

Regards,
Guest








PostPosted: Wed Feb 01, 2006 4:28 pm     Reply with quote

Just print it!....
printf("%5LD",val);

Will output the five ascii text digits to the current com device. Using sprintf instead, will allow these to be sent to a string. This is base 'C', nothing special to CCS.

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Feb 01, 2006 11:40 pm     Reply with quote

Quote:

I need to convert 16 bit hex number into BCD 5 digit.

The question itself has a conceptual mistake as well pointed by RJ. Binary Coded Decimal (BCD)
is a coding system in which each decimal digit from 0 to 9 is represented by a 4-digit binary number.
Also called packed decimal, this is the representation of a number by using four binary bits
numbers.
So the number 29 would be encoded as 0010 1001 instead of its binary equivalent 0001 1101

Guest wrote:
Quote:

Just print it!....
printf("%5LD",val);

The bin/dec convertion is made ONLY while outputing a char to console, it is not available for further use.

Fabri wrote:
Quote:

Is there any function in CCS compiler to do it ?

There is not any CCS function to do this convertion.

Fabri wrote:
Quote:

I need to convert an 16 bit hex number , for example 0xffff, to decimal 5 digit as 6 5 5 3 5.


I wrote the following code to use in a display:
Code:


long Dig_0, Dig_1, Dig_2, Dig_3, Dig_4;

void bin_to_Dig(long bin_inp)   // 0x1234
{
long temp;

   temp  = bin_inp;
   Dig_4 = temp/10000;
   temp  = temp - (Dig_4 * 10000);

   Dig_3 = temp/1000;
   temp  = temp - (Dig_3 * 1000);

   Dig_2 = temp/100;
   temp  = temp - (Dig_2 * 100);

   Dig_1 = temp/10;
   temp  = temp - (Dig_1 * 10);

   Dig_0 = temp;
}


Output results:
Code:

 Binary Inp         Display Out
 
   0x0064              00100
   0x00AA              00170
   0x0100              00256
   0x1000              04096
   0x3FFF              16383
   0xFFFF              65535


Humberto
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 1:28 am     Reply with quote

Hi,

I decided to use the way of sprintf to a string and then I convert all character to number.

Thanks for help,

Regards
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Re: bcd Conversion of 16bit number
PostPosted: Thu Feb 02, 2006 1:38 am     Reply with quote

Fabri wrote:
Hi Everibody,

I need to convert 16 bit hex number into BCD 5 digit.

Regards,


I suspect your looking for a 5 digit non packed(4 digit) bcd

try this

char bcd_string[5];

sprintf(bcd_string,"%lu",16_bit_number)

now each element of bcd_string contains the 10^0 to 10^4
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 1:51 am     Reply with quote

Hi Eugeneo,

I did so,

Regards,
Ttelmah
Guest







PostPosted: Thu Feb 02, 2006 3:44 am     Reply with quote

One caveat.
The string storage area, should be _six_ characters long, not the five being shown above. Remember in C, 'strings' are just arrays of bytes, with a '0' terminator (numerical zero, not ASCII '0'). To store a five character string, you must have six available spaces, or the terminator will overwrite something else....

Best Wishes
Ttelmah
Guest







PostPosted: Thu Feb 02, 2006 3:44 am     Reply with quote

One caveat.
The string storage area, should be _six_ characters long, not the five being shown above. Remember in C, 'strings' are just arrays of bytes, with a '0' terminator (numerical zero, not ASCII '0'). To store a five character string, you must have six available spaces, or the terminator will overwrite something else....

Best Wishes
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 3:52 am     Reply with quote

Hi,

Of course, I use 5 digit with a string of 6 char.

Thanks,
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 11:23 pm     Reply with quote

Thats right. I would have had the screwup fairy visit me on that one. Most string operations including sprintf terminate with a null.

Just for argument sake, would the null be inserted in next compiler assigned memory location? or would sprintf be linked to the upper bound of the char definition and null terminate premature?
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