Good BCD to Decimal code?
Ravenkallen
Posts: 1,057
I am looking for an efficient way to make a routine that will be able to convert a bcd value(0-255) to decimal. My current coding uses a ton of IF statements to get the job done and it is slow. Does anybody have a simple snippet of a method that can convert between the two? Just a little something to get me back on track. It is probably really trivial, but i am just having a hard time visualizing it..
Comments
It is 1 digit for every four bits(So each byte contains 2 digits) . It is the format commonly used in RTC's . So i need to convert a number that holds 2 bcd digits to a single byte value
And Kuronekos code is correct if you call the function correctly. It only allows you to call it with $0000_00_00 - $0000_09_99. In your special case you only want to call it with $000-$255, so that should not be a problem.
What kuronekos code is doing for example with number $128:
$128 >> 8 = 1
1*100=100
$128>>4 = $12
$12 & %1111 = 2
2*10=20
$128 & %1111 = 8
and finally 100 + 20 + 8 = 128 = $80