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
  • This wouldn't be homework would it?

    Firstly, an 8-bit value will be in the range 0 to 255, so obviously some values require more than 2 decimal digits.

    To convert a value in the range 0..99, simply divide the value by 10 to obtain the most significant digit and take the modulo 10 value to obtain the least significant digit. The DIV instruction will all this in one go.

    Compacting two digits into one byte requires moving some bits and ORing the results together.

    To convert a decimal digit to ASCII, you just have to add a magic value.

Reply
  • This wouldn't be homework would it?

    Firstly, an 8-bit value will be in the range 0 to 255, so obviously some values require more than 2 decimal digits.

    To convert a value in the range 0..99, simply divide the value by 10 to obtain the most significant digit and take the modulo 10 value to obtain the least significant digit. The DIV instruction will all this in one go.

    Compacting two digits into one byte requires moving some bits and ORing the results together.

    To convert a decimal digit to ASCII, you just have to add a magic value.

Children