DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Decimal to Binary Conversation stored into a char array (http://www.daniweb.com/forums/thread179779.html)

djextreme5 Mar 3rd, 2009 8:58 pm
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??

rgfirefly24 Mar 3rd, 2009 9:03 pm
Re: Decimal to Binary Conversation stored into a char array
 
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)

djextreme5 Mar 3rd, 2009 9:06 pm
Re: Decimal to Binary Conversation stored into a char array
 
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.

MosaicFuneral Mar 3rd, 2009 9:27 pm
Re: Decimal to Binary Conversation stored into a char array
 
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?

djextreme5 Mar 3rd, 2009 9:38 pm
Re: Decimal to Binary Conversation stored into a char array
 
Quote:

Originally Posted by MosaicFuneral (Post 817691)
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?

MosaicFuneral Mar 3rd, 2009 9:58 pm
Re: Decimal to Binary Conversation stored into a char array
 
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?

blacklight332 Mar 3rd, 2009 10:45 pm
Re: Decimal to Binary Conversation stored into a char array
 
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
}


All times are GMT -4. The time now is 4:58 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC