Ok, so I'm writing a program to convert decimal into binary. It all works with the exception that my binary shows up in reverse order and I'm having trouble with reversing the array of chars in my intToBinaryString() function.

Any tips on how to get this started?

#include<iostream>
#include<cstring>

using namespace std;

void intToBinaryString(long int, char []);

#define ARRAY_SIZE 10001

int main()
{
    long int number;
    char binary[ARRAY_SIZE];
    int count = 0;

    cout << "Enter a non-negative integer to convert to binary: ";
    cin >> number;
    if(number >= 0)
    {
        cout << "The binary representation for " << number << " is: ";
        intToBinaryString(number, binary);
    }
    else
    cout << "Enter a non-negative number" << endl;
    cout << endl << endl;
    
    system ("pause");
    return 0;
}

void intToBinaryString(long int num, char bin[])    //NOTE:check pg 817 for c-string info
{
    int remainder, n=0;
    char tempArray[ARRAY_SIZE];
    
    do
    {
        remainder = num%2;
        num = num/2;
        itoa(remainder, tempArray, 10);
        cout << tempArray;           
    }while(num > 0);                      
}

Recommended Answers

All 5 Replies

Get the cout statement out of the do-while loop. If you want to display tempArray, do it after intToBinaryString has finished. A function should convert or display, but not both unless there is a reason IMO. Either use itoa or use a loop, but don't use itoa inside a loop.

You are isolating a bit at a time, which is fine, but you should then be isolating a character in that string. Some nice, easy to read code would be the following:

int index; // initialize to something
    do
    {
        remainder = num%2;
        num = num/2;

        if(remainder == 0)
        {
            tempArray[index] = '0';
        }
        else
        {
            tempArray[index] = '1';
        }

        // adjust index
        
    }while(num > 0);

That's one of many ways to do it. I'm not sure you need a temporary array at all, and if you initialize/change index correctly, there will be no need to reverse any string. But to reverse a string, a simple loop will suffice:

int len = strlen(tempArray);
int max = len / 2 - 1;

for(int i = 0; i <= max; i++)
{
    char temp = tempArray[i];
    tempArray[i] = tempArray[?]; // fill in ?
    tempArray[?] = temp; // fill in ?
}
commented: great example, algorithm worked perfectly +0

On a side note; you could use std::bitset to do the conversion relatively easy:

#include <iostream>
#include <bitset>

int main() { 
    unsigned long in = 1234567;
    std::bitset<32> out(in);
    std::string out_str = out.to_string();
    std::cout << out_str;
}
#include <string.h>
#include <stdio.h>

int main(void)
{
   char *forward = "string";

   printf("Before strrev(): %s\n", forward);
   strrev(forward);
   printf("After strrev():  %s\n", forward);
   return 0;
}

use strrev(char *ch) to reversing the charter array.

Thanks, this helped a bunch. The program is working great now!

Get the cout statement out of the do-while loop. If you want to display tempArray, do it after intToBinaryString has finished. A function should convert or display, but not both unless there is a reason IMO. Either use itoa or use a loop, but don't use itoa inside a loop.

You are isolating a bit at a time, which is fine, but you should then be isolating a character in that string. Some nice, easy to read code would be the following:

int index; // initialize to something
    do
    {
        remainder = num%2;
        num = num/2;

        if(remainder == 0)
        {
            tempArray[index] = '0';
        }
        else
        {
            tempArray[index] = '1';
        }

        // adjust index
        
    }while(num > 0);

That's one of many ways to do it. I'm not sure you need a temporary array at all, and if you initialize/change index correctly, there will be no need to reverse any string. But to reverse a string, a simple loop will suffice:

int len = strlen(tempArray);
int max = len / 2 - 1;

for(int i = 0; i <= max; i++)
{
    char temp = tempArray[i];
    tempArray[i] = tempArray[?]; // fill in ?
    tempArray[?] = temp; // fill in ?
}

Hi bmos31 can you explain where is the bunch in my code, I am very thankful to you, if point out my error.

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.