# Newbie Tech: Hex to BCD conversion

#### Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

•  Subject
• Author
• Posted on
Having an old 8 bit ASM converted to newer (ST7) 8-bit ASM.  ST does not
have a BCD mode (which the older processor did), so I need to convert
arithmetic (mostly adds with carry) from hex to BCD, and back.

The BCD uses a 4 bit nibble that does not exceed 10 decimal.  In this
application, the BCD is primarily used for 7 segment LED displays (to keep
from trying to have an A-F put out to the LED drivers).

Question:

Is it difficult to explain how you would do this conversion in a system
(ST7) that only works in Hex?

Now that I have re-read this note, I realize acronyms were made to keep
people from communicating with each other ;-)

Jim Knight
remove NOSPAM from direct email responses

Re: Newbie Tech: Hex to BCD conversion

This sounds as if you don't care what base values are displayed
in.  Conversion to BCD is isomorphic to conversion to an Ascii
string.  However you might consider Octal displays, which simply
have to take 3 bits at a time, and use only digits 0 through 7.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: Newbie Tech: Hex to BCD conversion

No, not difficult. All you really need to do is to look at what happens to one
nibble. This assumes you added two legal BCD values together into a sum.

First, you need to detect when the nibble goes beyond 9. So, what do you do?
Try an example:

1001  9
0001  1
1010  10(hex)  Illegal BCD

What we want is 10hex. To get this, we need to subtract 10(hex) from this
nibble and add 1 to the next higher nibble. If we are looking at the least
significant nibble of a byte, we see that this means adding 16 to increment the
high nibble. So, +16-10 = +6. We come out with this general rule:

1. If the low 4 bits of the sum are > 9, add 6 to sum.

But we have to take another case into account. What if the low nibble of the
sum is legal BCD, but there was a carry from the low nibble to the high during

01001  9
01001  9
10010  12(hex) = 18 decimal

What we want is 18(hex). We need to add 6 to the sum to make it right. Which

2. If there was a carry from low nibble to high, add 6 to the sum.

Fortunately, the ST7 has a carry bit to show a carry from the high nibble, and
a half-carry bit to show a carry from the low nibble to the high nibble.

I'll leave BCD subtraction to you, though you probably don't need that.

Re: Newbie Tech: Hex to BCD conversion
Gary,
Thanks.  Appreciate the clear instructions even someone like me can
understand.  Guess I should be able to write a routine that does this and
then just call the routine whenever the original code is operating in
Decimal mode.

Again thanks.

Jim Knight

one
do?

increment the
the
during
Which
and

Re: Newbie Tech: Hex to BCD conversion
Sorry if this is obvious

Don't know if this applies, but notice that if you're doing multiple
digit hex numbers, it's not enough to just convert the hex nibbles to
their BCD equvalents if what you want is to end up with actual BCD

Suppose you've got a two digit hex number.  You've got a 1's place
and a 16's place. If you simply convert that 16's place hex digit
to BCD you end up with (sort of) BCD coded hex.  You'll need to multiply
it by 16 to get the BCD.

C0 in HEX which is 192 in decimal

if you only convert the high nibble to it's BCD equvalent you get

120  in BCD (or "BCD coded hex")

You have to multiply that 12 by 16

Re: Newbie Tech: Hex to BCD conversion
Hi, the simplest way is to use hex for all your operations. Convert to bcd only
for the led output.