Tuesday 24 March 2015

Decaimal to BCD number conversion.

I was recently contacted to write a program that converts numbers form Decimal to BCD representation.

The code shown works in two steps.
  1. Converts the binary number to decimal
  2. Converts the Decimal number to Binary. 
Just a recap:
In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system, or base-2 numeral system, which represents numeric values using two different symbols: typically 0 (zero) and 1 (one). (Wikipedia)

To know more about BCD numbering system, Click here.

The code captures a string input form a user, accepts 8 digit binary numbers. The string is then converted to an array on integers containing 1s and 0s. Using this array, a decimal representation of the binary number is formed.

From the decimal representation of the number, the BCD equivalent can be created.

Below is a print screen of the running program:
Below is the running code.

Please drop a comment and how the programe can be improved.

If you think you have a better way of creating this, let us know, post a link to your code of comment with your code.


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

/**************************************************************************************/
/*Program converts a binary number to BCD number representation ***********************/
/* Programe first converts the binary number to decimal, then the decima to BCD *******/
/****Created by Joshua Waiswa, jwaiswa7@gmail.com, +256794865174 **********************/
/**************************************************************************************/

int main(){
   
    char binaryString[8];
    string binaryRep [] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001"};
    string BCDOutPut [] = {};
    int binary[8] = {0,0,0,0,0,0,0,0};   
   

    int binaryArray [3];
    int i, x;
    int decimal;
   
    cout<<"Enter an 8 digit binary number\n>";
    cin >> binaryString; //User input comes in as a string of 1s and 0s

    /*Converting the characters to integers for operation*/
    for ( x = 0; x<8; x++){
    binary[x] = (int)binaryString[x] - (int)48;
    }

    /*Binary string converted to an array of 1s and 0s */
    cout << "You entered: ";

    for (i = 0; i<8; i++){
   
    cout << binary[i];
    }

    /*Converting the binary number to decimal*/
   
    decimal = (binary[0]*(2*2*2*2*2*2*2))+(binary[1]*(2*2*2*2*2*2*2))+(binary[2]*(2*2*2*2*2))+(binary[3]*(2*2*2*2))+(binary[4]*(2*2*2))+(binary[5]*(2*2))+(binary[6]*(2))+(binary[7]*(1));

    cout << endl<<"Decimal representation: " << decimal << endl <<"BCD Representation: ";
   

    /*converting the decimal number to BCD */
    /*Getitng the components of the Decimal number */
   
    binaryArray[2] = decimal %10;
    decimal = decimal/10;
    binaryArray[1] = decimal %10;
    decimal = decimal/10;
    binaryArray[0]= decimal%10;

   
    /*Converting the Decimal number to BCD */
    int z;

    for (z =0; z<3;z++){
       
    int t = binaryArray[z];
   
    switch(t){
   
    case 0:
        cout << binaryRep[0];
    break;
    case 1:
        cout << binaryRep[1];
    break;
    case 2:
        cout << binaryRep[2];
    case 3:
        cout << binaryRep[3];
    break;
    case 4:
        cout << binaryRep[4];
    break;
    case 5:
        cout << binaryRep [5];
    break;
    case 6:
        cout << binaryRep [6];
    break;
    case 7:
        cout << binaryRep[7];
    break;
    case 8:
        cout << binaryRep[8];
    break;
    case 9:
        cout << binaryRep[9];
    break;
    default:
        cout << "Error funny result" << endl;   
    }   

    }
   
    cout << endl;   

    return 0;
}