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
  • the above should be

    unsigned char chartobcd(unsigned char )
    {
      if (n >99) crash();
      return ((n / 10) << 4) | (n % 10);
    }

    While the code, indeed, works, any function that can not handle all values of the input (255 for a byte, ffff for an int) should trap invalid input values. Bugs occuring from someone seeing the above function prototyped and use it can be lenghty to trap.

    Erik

Reply
  • the above should be

    unsigned char chartobcd(unsigned char )
    {
      if (n >99) crash();
      return ((n / 10) << 4) | (n % 10);
    }

    While the code, indeed, works, any function that can not handle all values of the input (255 for a byte, ffff for an int) should trap invalid input values. Bugs occuring from someone seeing the above function prototyped and use it can be lenghty to trap.

    Erik

Children