DEC to BCD (conversion)

I have to convert a decimal number (1-255) to packed binary-coded decimal.

I have the idea of how it's supposed to work.

Example:
Convert 133 to BCD
1. Separate 133 to individual numbers (1, 3, 3).
2. Convert each number to binary (0001, 0011, 0011).
3. Print it in the form of "133 - 0001 0011 0011".

However, I'm struggling how to get the number separation and whatnot.
I'm just really confused how to program this.

Also, the decimal numbers have to be in 3 values, (eg; 4 has to be 004)
Because the result to BCD should be 0000 0000 0100, rather than just 0100.

If someone can help me with some C++ algorithm or snippet of code with some explanation, I would really appreciate it.
Use integer division and remainder to separate out the digits of a number.

174 % 10 -->  4
174 / 10 --> 17

A zero is always a zero, so dividing zero gives zero. If you are required to have three digits, just assume that there are three digits there.

Good luck!
Last edited on
Thanks Duoas, that works, but is there even ANY way of getting the 3 zeroes there?

With this code (decimals 1-10), my result is this:

0 0 0
0 0 1
0 0 10
0 0 11
0 0 100
0 0 101
0 0 110
0 0 111
0 0 1000
0 0 1001


I need it to be:

0000 0000 0000
0000 0000 0001
0000 0000 0010
0000 0000 0011
0000 0000 0100
0000 0000 0101
0000 0000 0110
0000 0000 0111
0000 0000 1000
0000 0000 1001


CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

void binaryConv(int);

int main()
{
	int i, a, b, c;

	for (i = 0; i < 10; i++)
	{
	a = i / 100;
	b = (i / 10) % 10;
	c = i % 10;
	binaryConv(a);
	cout << " ";
	binaryConv(b);
	cout << " ";
	binaryConv(c);
	cout << endl;
	}

	system("pause");
}

void binaryConv(int num) 
{
	int remain;

	if(num <= 1) {
		cout << num;
		return;
	}

	remain = num%2;
	binaryConv(num >> 1); 
	cout << remain;
}
Last edited on
Good job! To get four digits you have several options, but just #including <iomanip> will do and using the following:

1
2
3
4
5
cout << setfoll('0');

...

cout << setw(4) << num;

:-)
Thank you Duoas again, however, I still have a problem with the option you provided.
Upon the best estimated location of putting these 2 line of codes, I was only able to get a result like this:

0000 0000 0000
0000 0000 0001
0000 0000 00010
0000 0000 00011
0000 0000 000100
0000 0000 000101
0000 0000 000110
0000 0000 000111
0000 0000 0001000
0000 0000 0001001
Oops, I forgot you were using multiple couts to print each number.

Er, you'll have to append things to a string instead of using cout. Then print the string...
I didn't learn much in strings, so I may need help with that.
I'm just trying to convert the binaryConv function to string.

I'm just struggling with a bunch of errors like "no operator "<=" matches these operands", so on so forth. Can you point me to the right direction using strings?
Instead of cout << num; use s += (char)(num + '0'); or s += (char)(remain + '0');

Is there a reason your function is recursive? You might find it easier to do if it isn't... But if you do keep it recursive, you'll have to build your string recursively also. For example, here is something to play with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string binaryConv(int num)
{
	if (num <= 1)
	{
		return string() + (char)('0' + num);
	}

	int remain = num % 2;
	return binaryConv(num / 2) + (char)('0' + remain);
}

int main()
  {
  cout << "Enter a number to be converted to binary\n> " << flush;

  string s;
  while (getline( cin, s ) && !s.empty())
    {
    istringstream ss( s );
    unsigned n;
    ss >> n;
    if (!ss.eof()) break;

    cout << setfill( '0' ) << setw( 4 )
         << binaryConv( n ) << "\n> " << flush;
    }

  return 0;
  }

You could very easily make that binaryConv() function simpler... but I'll leave it to you.

Enjoy!
Oh wow! Thanks for the reply Duoas, but this is beyond my knowledge at the moment.
Can you explain what's going on in this? s += (char)(num + '0'); and as well as in the main code. Very unfamiliar with strings, I just need some explanation on what stuff like the getline is doing, what is istringstream ss (s), same with "unsigned". I'm just entirely confused.

I hate to make you do much more, but I need to figure out what everything does, then I can make some changes, like getting rid of recursion (like you partially recommended), as well as adding a for loop (needs to display 1-255) instead of the while.

I can try make some assumptions myself;
while (getline( cin, s ) && !s.empty())
- Does this mean it gets the information from what the user typed and gets defined as string s?

Thanks a lot Duoas, I really appreciate your help
Last edited on
It doesn't matter what all the getline() and stringstream stuff does. You need to concentrate on your program. I just gave an example to play with.

The ASCII character '0' is the same as (char)('0' + 0).
The ASCII character '1' is the same as (char)('0' + 1).

Work on making it non-recursive and you'll have a much better solution (and one you will understand easier).

Good luck!
Topic archived. No new replies allowed.