![]() |
![]() |
![]() |
|
|
|||||||
| General Electronics Chat This forum is for general chat about electronics, eg: Dont know what a part does? Dont know how to read a circuit? Want to get an opinion? |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
What mathematical formula should i use to convert an 8 bit binary to BCD?
for example in 4 bit binary, i just add 0110 to the binary if it is >1001 to produce the BCD. i realise this formula doesn't work when the decimal equivalent of the binary is 20 or more. |
|
|
|
|
|
|
(permalink) |
|
Try this algorithm
Let N = the binary number to be converted, for an 8 bit number, N < 256. I'll use decimal numbers rather than binary for simplicity. Let H = the hundreds digit, T = the tens digit, U = the units digit. Initialise H = 0, T = 0 A If N < 100 then GOTO B N = N - 100, H = H + 1, GOTO A (at the end of this loop, H = the hundreds digit) B If N < 10 then GOTO C N = N - 10, T = T + 1, GOTO B (at the end of this loop, T = the tens digit) C U = N (ie. the remainder is the units digit) So the BCD number is H T U Len |
|
|
|
|