Unsigned Integer to BCD conversion?

前端 未结 10 1469
囚心锁ツ
囚心锁ツ 2020-12-11 05:01

I know you can use this table to convert decimal to BCD:

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 01

10条回答
  •  孤街浪徒
    2020-12-11 05:38

    This is from the micro controller world.... Note that values are rounded in the division. For instance 91 to BCD would be 91/10 * 16 = 144 + 91%10 = 145. Converted to Binary is 10010001.

    uint8_t bcdToDec(uint8_t val)
    {
      return ( (val/16*10) + (val%16) );
    }
    
    uint8_t decToBcd(uint8_t val)
    {
      return ( (val/10*16) + (val%10) );
    }
    

提交回复
热议问题