954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Decimal to Binary Conversation stored into a char array

Hello, I am trying to create a program where I can work with an array of char that contain either 0 or 1 representing a decimal number that the user enters.

Basically I need to convert a decimal number into binary and store it into a char array. I wrote this program but it is outputting something weird.

char binary(int number)
{
	int temp = 0, c = 0;
	char buffer;

	if (number % 2 == 0)
	{
		temp = number / 2;
	}
	else
	{
		number = number - 1;
		temp = number / 2;
	}
	
	if (temp % 2 == 0)
	{	
		buffer = '0';
	}
	else
	{
		buffer = '1';
	}

	return buffer;
}

int main()
{
	int num;

	cin >> num;

	while(num != 0)
	{
		cout << binary(num);
		
		if (num % 2 == 0)
		{
			num = num / 2;
		}
		else
		{
			num = num - 1;
			num = num / 2;
		}
	}

	return 0;
}

Can someone help me out??
djextreme5
Junior Poster
104 posts since Mar 2009
Reputation Points: 76
Solved Threads: 6
 

try something along the lines of itoa which should allow you to use base 2 to convert it to binary

it would look like so: itoa(temp, chararray, 2)

rgfirefly24
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

I cannot use itoa();

I already did that and it won't compile on my teacher's computer. Apparently she is using some website's judging process and it has a g++ compiler. Since itoa() is not a standard function, g++ doesn't support it.

djextreme5
Junior Poster
104 posts since Mar 2009
Reputation Points: 76
Solved Threads: 6
 

uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0".
Simple?

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 
uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0". Simple?

What do you mean?

djextreme5
Junior Poster
104 posts since Mar 2009
Reputation Points: 76
Solved Threads: 6
 

Lets say you have the value ten, and in binary that is represented as "00001010." Mask(AND operator) all the bits before the first one and the truth is now false, place a zero to the array; shift the original value right once and mask again, this time the truth is true, add an "1"; repeat.

Well the array would look like "01010000" if you did that, there's an easy fix or just reverse it. If that's to much for you, can you use C++'s bitset class container?

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

just write your own itoa.

#include <iostream>
#include <string>

using namespace std;

string itoa(const int &integer,int base=10)
{
    if (integer==0) return string("0");

    string a;
    int start, digits, piece;

    //count digits
    digits=0;
    piece=((integer<0)? 0-integer : integer);
    while( piece > 0 )
    {
        piece-= (piece%base);
        piece/=base;
        digits++;
    }

    start=((integer<0)? 1 : 0);
    a.resize(digits+start,' ');//allocate memory only once
    if (integer<0) a[0]='-';

    piece=((integer<0)? 0-integer : integer);
    for(int i=0;  piece > 0; i++ )
    {
        a[ digits+start-i-1] = (piece%base)+48;
        piece-= (piece%base);
        piece/=base;
    }

    return a;
}


int main(int argc, char*argv[])
{
    cout << itoa(55) << endl; //defaults to base 10
    cout << itoa(0,2) << endl;// 0
    cout << itoa(1,2) << endl;// 1
    cout << itoa(2,2) << endl;// 10
    cout << itoa(3,2) << endl;// 11
    cout << itoa(4,2) << endl;// 100
    cout << itoa(5,2) << endl;// 101
    cout << itoa(63,2) << endl;// 111111
    cout << itoa(-1,2) << endl;// -1
    cout << itoa(-2,2) << endl;// -10
}
blacklight332
Newbie Poster
6 posts since Nov 2008
Reputation Points: 21
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You