0

We were asked to make function that that converts Decimal numbers into Binary and takes an integer as an input but must give a string as an output. How do I store the array into one string?

string DecToBin(int num)
{

    string res;

    for (int n = 15; n >= 0; n--)
    {
        if ((num - pow(2, n)) >= 0)
        {
            res[n] = 1;
            num -= pow(2, n);
        }
        else
        {
            res[n] = 0;
        }

    }

    for (int n = 15; n >= 0; n--)
    {
        res[n];
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Renz
  • 37
  • 7
  • 1
    When you declare the string it's of size zero, which means doing e.g. `res[n]` will be indexing out of bounds for *every* `n`, leading to [*undefined behavior*](http://en.wikipedia.org/wiki/Undefined_behavior). However, if you read e.g. [this `std::string` reference](http://en.cppreference.com/w/cpp/string/basic_string) I'm sure you can find something which *appends* characters to a string. – Some programmer dude Dec 13 '14 at 11:40
  • And also, `res[n] = 1` is wrong you need to assign to a character like this `res[n] = '1'` – Iharob Al Asimi Dec 13 '14 at 11:42
  • 1
    Also, while I'm sure the `pow` function have a special case when the first argument is `2`, the usual way is to use the shift operator, e.g. `1 << n`. Also, do you have a check that the number you get as argument is no more than 16 bits? On most platforms today, `int` is 32 bits. – Some programmer dude Dec 13 '14 at 11:47
  • @JoachimPileborg I'm new to the shift operator, and no I currently don't have any function to check if the argument is more than 16 bits, but I will work on that later – Renz Dec 13 '14 at 11:56

4 Answers4

1

You just return the string

return res;

that's it.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

You could do something like this,

#include <iostream>
#include <string>

std::string foo(int n)
{
    std::string s;
    while(n > 0){
        s += n%2 == 0 ? '0' : '1';
        n = (n >> 1);
    }
    return std::string(s.rbegin(), s.rend());
}

int main()
{
    int n = 13;
    std::cout << foo(n) << std::endl;
    return 0;
}

Prints

1101
jensa
  • 2,792
  • 2
  • 21
  • 36
1

As you have begotten answer for how to return the result I add a supplement on the generation of the string.

As an alternative you can include <climits> which gives you CHAR_BIT and then calculate number of bits by e.g.:

#include <climits>

int bits = CHAR_BIT * sizeof(int);

or you can use <limits> and say something like:

#include <limits>

int bits = std::numeric_limits<int>::digits;

Then it is only to start the loop from the end:

string int2bitstr(int n) 
{
    int bits = numeric_limits<int>::digits;
    string s;
    int i;

    for (i = bits - 1; i >= 0; --i)

And say something like:

        s += n & (1 << i) ? '1' : '0';

or:

        s += (n >> i) & 1 ? '1' : '0';

or the bit (no pun intended) quicker:

        s += ((n >> i) & 1) + '0';

Reason the last one work is because one add character value of the glyph 0 to the number 0 or 1 – in effect giving '0' or '1'.

Morpfh
  • 4,033
  • 18
  • 26
0

Just add

return res;

to the end of your function.

Mohammad Alshaar
  • 523
  • 4
  • 21