8 bit binary to BCD conversion

Status
Not open for further replies.
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.
 
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
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…