This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Need BINARY to BCD function

Hi,

Does anyone know how to convert a BINARY 8 bit to a 2 digit packed BCD.

Example if I have value 0x0C (wich is 12 decimal) the result will be 12 or a char value of 0x12.


thank you,

Adi,

Parents
  • Hi,

    Thanks for the HELP. No its not homework...I finished school long time ago :-) It was for communicating with a DALLAS RTC DS1337.

    Is your code better than this one ? I mean will it run faster ?

    Because I didn't use the MODULO 10, here is what I did:

    char bin2BCD8(char cValue)
    {
    char cDig10=0;
    char cDig1=0;
    char cTemp;
    cTemp=cValue; // Backup the value
    do{
    cTemp=cTemp-10;
    if(cTemp<0)
    {
    break;
    }
    else
    {
    cDig10++;
    }
    }while(1);
    cDig1=cValue-(cDig10*10);
    cDig10=(cDig10<<4)|cDig1;
    return cDig10;
    }

Reply
  • Hi,

    Thanks for the HELP. No its not homework...I finished school long time ago :-) It was for communicating with a DALLAS RTC DS1337.

    Is your code better than this one ? I mean will it run faster ?

    Because I didn't use the MODULO 10, here is what I did:

    char bin2BCD8(char cValue)
    {
    char cDig10=0;
    char cDig1=0;
    char cTemp;
    cTemp=cValue; // Backup the value
    do{
    cTemp=cTemp-10;
    if(cTemp<0)
    {
    break;
    }
    else
    {
    cDig10++;
    }
    }while(1);
    cDig1=cValue-(cDig10*10);
    cDig10=(cDig10<<4)|cDig1;
    return cDig10;
    }

Children