Electronic Design

# Easily Convert Decimal Numbers To Their Binary And BCD Formats

Here's a C/C++ program that converts decimal numbers ranging from 0 to 99,999 to binary and BCD formats. Using a simple algorithm in conjunction with pointer arithmetic and bitwise shifting increases the conversion speed without introducing excessive memory overhead and programming complexity.

When decimal numbers are within the range of 0 to 9, their binary and BCD representations are identical, requiring only four bits (0000 to 1001). When the numbers are within the range of 10 to 19, the binary representation varies between four and five bits, such as 1010 to 10011. The BCD representation uses eight bits, such as 0001 0000 to 0001 1001.

If we treat the BCD representation as a straight binary number and compare it to the actual binary representation of the decimal value, we note that there's a decimal difference of 6 between the two numbers. The actual decimal number varies between 10 and 19, while the BCD decimal representation varies between 16 and 25. Increasing the actual decimal range to numbers between 20 and 29 results in a difference of 12 between the binary and BCD representations.

This process continues to 99—such that the difference is equal to the most significant decimal digit multiplied by 6. Thus, 90 decimal would be represented in BCD format by 90 + 9*6 = 144. Decimal numbers between 100 and 109 would be represented in BCD by the actual number + 156 (26*6). Numbers between 110 and 119 convert to BCD values of the actual number + 156 + 6.

If we continue this representational process for decimal range of 0 to 99,999, the algorithm developed for the BCD decimal number is defined by the following equation:

`dbcd = (m*9256 + I*516 + b*26 + cc)*6 + x`

In this equation, x = the actual decimal value; cc = the integer value of (x modulo 100)/10; b = the integer value of (x modulo 1000)/100; I = the integer value of (x modulo 10000)/1000; and m = the integer value of x/10000.

The program uses two one-dimensional arrays to temporarily store the binary (c\[18\]) and BCD (bcd\[20\]) numbers, because they're generated by looping bit-by-bit within their separate conversion functions. The actual bit generation assigned to the appropriate array position is accomplished by right bit shifting, via the array index, the decimal number that's to be converted. This shifted number is then masked or "ANDed" with the hex value of I. Each of these bits is added to the array via their associated pointers. The binary conversion function has an index of 0 to 18, while the BCD conversion function uses an index of 0 to 20. The decimal range of 0 to 99,999 determines these indices.

The program also contains a function called LOCATE, which enables the cursor on the computer display to be positioned at any row or column within the visible screen. The remainder of the program has various print statements that let the binary, BCD, decimal, and BCD-decimal numbers be formatted and aligned in a neat tabular form (see the figure).

For most computers, that actual decimal number range could be easily extended to a value of 232, which is the limit for unsigned long integer values. The equation dbcd would require modification to account for the additional boundary changes as the number increases beyond the value of 99,999. Click here to see the complete program listing (DecimalConversion.doc). 