Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned Integer to BCD conversion?

Tags:

c++

bcd

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 0110

7 0111

8 1000

9 1001

Is there a equation for this conversion or you have to just use the table? Im trying to write some code for this conversion but Im not sure how to do the math for it. Suggestions?

like image 666
Bramble Avatar asked Sep 11 '09 00:09

Bramble


People also ask

How is BCD value calculated?

Simply divide the binary number into groups of four digits, starting with the least significant digit and then write the decimal digit represented by each 4-bit group.

Is BCD and 8421 same?

8421 Code or BCD Code As per the weighted binary digits, the 4 Bit binary numbers can be expressed according to their place value from left to right as 8421 (2³ 2² 2¹ 2⁰ = 8421).


1 Answers

You know the Binary numeral system, don't you?

Especially have a look at this chapter.

EDIT: Also note KFro's comment that the lower nibble (= 4 bits) of the binary ASCII representation of numerals is in BCD. This makes conversions BCD <-> ASCII very easy as you just have to add/remove the leading 4 bits:

Number    ASCII Code
0         0011 0000
1         0011 0001
 ...
8         0011 1000
9         0011 1001
like image 118
schnaader Avatar answered Oct 03 '22 11:10

schnaader