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??``````

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)

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.

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?

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?

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?

``````#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
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.