i m using Keil, and C language
the problem is that, i have one decimal number, like 27, that will be 11011 in binary, but if i split the 27 then it will be "2" and "7" , in binary, "10" and "111", i want to convert it to "00100111", the first four bits(right site) for 7 and other 4 for 2.
The principle of the conversion from binary to BCD (Binary Coded Decimal) you can grasp from my commented example (in C) shown below:
it's conversion of a binary unsigned integer (in the example the binary value must be less than 10000!) to the corresponding 'condensed' BCD.
/*----------------------------------------------------------------------------*/
/* Conversion of "unsigned int" (BIN) to condensed BCD, range 0 to 9999 only! */
/*----------------------------------------------------------------------------*/
unsigned int Bin2BCD(unsigned int bin) // bin < 10000!
{
unsigned int bcd;
unsigned char aux;
aux = bin / 1000;
bin -= aux * 1000;
bcd = aux << 4;
aux = bin / 100;
bin -= aux * 100;
bcd |= aux;
bcd <<= 8;
aux = bin / 10;
bin -= aux * 10;
aux = (aux << 4) | bin;
bcd |= aux;
return(bcd);
// condensed BCD word (in the end it contains the result)
// auxiliary variable
// number of 'thousands'
// remainder = input value "without 'thousands'"
// shifts 'thousands' in the upper nibble of lower byte
// number of 'hundreds'
// remainder = input value "without 'thousands' + 'hundreds'"
// 'hundreds' "joined" with 'thousands' (lower nibble of lower byte)
// shifts 'thousands' and 'hundreds' in the upper byte
// number of 'tens'
// remainder = 'units'
// 'tens' in the upper nibble, 'units' in the lower one
// 'thousands', 'hundreds', 'tens' and 'units' joined together