0
// C++ program to convert a decimal
// number to binary number

#include <iostream>
using namespace std;

// function to convert decimal to binary
void decToBinary(int n)
{
    // array to store binary number
    int binaryNum[1000];

    // counter for binary array
    int i = 0;
    while (n > 0) {

        // storing remainder in binary array
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }

    // printing binary array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << binaryNum[j];
}

// Driver program to test above function
int main()
{
    int n = 17;
    decToBinary(n);
    return 0;
}

So this is a program to convert Decimal numbers to Binary. Now I'm trying to convert Decimal Numbers to BCD. I get the concept, if I have a number like 215 for example, I separate each number [2,1,5] and then convert each number into binary so it would be 0010,0001,0101. I am just confused about implementing it.

Watercayman
  • 7,970
  • 10
  • 31
  • 49
300SRT
  • 59
  • 2
  • 5

4 Answers4

2

First of all, your algorithm simply displays the binary representation of some number n, instead of dividing it into single digits and returning some set of their binary representation.

To make out lives easier, we will be using standard containers and standard algorithms:

[...] if i have a number like 215 for example, i seperate each number [2,1,5] and then covert each number into binary so it would be 0010,0001,0101

Great, it means that we need some sort of a container to hold those three representations, don't we? My choice would be std::vector, since it is incredibly simple and efficient! You can read more about it here.

The mentioned vector will eventually store the binary representations, but here we encounter another problem - we actually need to somehow represent them!

Fortunately enough, the standard gives us a great tool - std::bitset, which is explained here. It is primarily used to make binary operations easier, but one of its great features is that it's also extremely good at simply being a binary representation.

The final function could look like this:

auto dec_to_bin(int n)
{
    std::vector<std::bitset<4>> repr;
    while(n > 0){
        repr.push_back(std::bitset<4>(n % 10));
        n /= 10;
    }
    std::reverse(repr.begin(), repr.end());
    return repr;
}

What is happening here?

We firstly create a vector of fixed size bitsets (of the size 4, since every decimal digit can be represented as four binary digits), then as long as our n is greater than zero (you already know why - you are using the same logic in your code), we add (using push_back) a new bitset, that will be treated as binary representation of modulo of your number (i.e. the last digit).

Keep in mind though, that by doing this, we created the vector in the reversed order. The last two things we have to do is simply reverse and return it!

Finally, we can use our function in main as such:

int main()
{
    for(auto b : dec_to_bin(215)){
        std::cout << b << ' ';
    }
}

This will print 0010 0001 0101, which was your desired output for the number 215

Fureeish
  • 12,533
  • 4
  • 32
  • 62
0

Can't you just replace the % 2 and / 2 with % 10 and / 10? The variables will be named wrong but that's the algorithmic change.

0

You simply have to divide your integer by digits and call your function for each digit:

void decToBCD(int n) {
    // array to store digits
    int digits[10];

    // counter for digits
    int i = 0;
    while (n > 0) {
        // storing remainder in digit array
        digits[i] = n % 10;
        n = n / 10;
        i++;
    }

    // printing binary representation of digits
    for (int j = i - 1; j >= 0; j--) {
        decToBinary(digits[j]);
        cout << " ";
    }
}
0

I would update what you have to return a string from decToBinary rather than printing to cout, you can then write decToBCD which uses modulo 10 to work out the integer for each digit of the number (in the same way you used modulo 2 and divide by 2 to get each bit in decToBinary), and call decToBinary for each integer digit and concatenates the strings of binary digits to give the full result.

Ian4264
  • 307
  • 2
  • 6