1

I am converting a decimal integer into a binary without using strings (in Visual Studio 2013):

int de2bi(int de)
{
int bi = 0, pos = 1;
while(de > 0)
{
    bi = bi + (de % 2) * pos;
    de = de / 2;
    pos *= 10;
}
return bin;
}

This way, the binary representation in output is actually a decimal number. The problem is that I want to control the number of digits in my binary output i.e. instead of 0 I want 00 or instead of 1 I want 01.

How can I convert the output into a vector/string with an appropriate size so that when converting decimal number '1', I can have '001' or '0001' in the returned output, depending on how many digits I need in my output?

Edit: I adopted the code from the answer down below. But it did not change the length of the output vector 'bin' in my code. It only prints '000' on the screen.

std::cout<<std::setw(3)<<std::setfill('0')<<bin;    
Elnaz
  • 233
  • 2
  • 9

2 Answers2

0
std::string de2bi(int de){
    std::string bin = "";
    if( de == 0 ){
        return "0";
    }
    else if( de < 0 ){
        return "";
    }

    while(de){
        bin = std::to_string(de % 2) + bin ;
        de = de / 2;
    }
    return bin; 
}

you can test at here http://cpp.sh/5cx

Mr Jerry
  • 1,686
  • 3
  • 14
  • 22
  • @ Mr Jerry, thank you. I am using std::string in = de2bi(3); and I want to see printf("%s ", in[0]); or printf("%s ", in[1]); How do I access them? – Elnaz Feb 10 '15 at 01:34
  • in case de===0 I don't necessarily want to return 0 but depending on the desirable size in my code I might want 00 or 000 or 0000. – Elnaz Feb 10 '15 at 01:38
  • you can use ``printf("%c ", in[1])`` – Mr Jerry Feb 10 '15 at 01:42
  • Yes that works for de2bi(3), but what if I want to convert decimals from 0 to 3 i.e. I want 00, 01, 10, 11 instead of 0, 1, 10, 11? How to generate that zero msb? I want to control the size of my returned string. – Elnaz Feb 10 '15 at 01:47
  • can you explain more? – Mr Jerry Feb 10 '15 at 06:52
0

Having called your de2bi function, you can display the result n as a w-character wide output as follows:

#include <iomanip>

std::cout << std::setw(w) << std::setfill('0') << n;

For example, if w is 3 and n is 1 you'll get 001.

There are many other S.O. questions and answers with more direct ways to display a number in binary, e.g. here.

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • for some reason I cannot use the << operator. I receive error: no operator "<<" matches those operands operand types are: std::ostream< – Elnaz Feb 10 '15 at 02:31
  • @Elnaz hmmm... did you also `#include `? If not - sorry for not mentioning it - I took that for granted. You shouldn't try to avoid `std::cout`... it's best practice in C++. If you're still having trouble, please post your exact code as an edit to your question and I'll happily take a look. Cheers. – Tony Delroy Feb 10 '15 at 02:35
  • I use std::string number=de2bi(0); Then I want to for example have my number to be 000 instead of just 0. I'm printf("%c", number[1]). The above comment was my mistake. But again using your line of code above did not change the length of n from 1 to 3. – Elnaz Feb 10 '15 at 02:40
  • I want to actually have a vector of length 3 instead of just the display. – Elnaz Feb 10 '15 at 02:55
  • @Elnaz: *"std::string number=de2bi(0);"* the `de2bi` function in your question did not return a `std::string`. My suggestion above is an alternative to "Mr Jerry"'s - not things you can combine. – Tony Delroy Feb 10 '15 at 03:04
  • BTW regarding *"it did not change the length of the output vector 'bin' in my code. It only prints '000' on the screen."* - `bin` in your code is an `int`... a number without any particular output representation... it's not an "output vector". You have to decide on the output format *each time* you output it to the screen, a file etc... If you want to capture that representation as a `std::string`, use `#include ` followed by `std::ostringstream oss; oss << std::setw(w) << std::setfill('0') << n; std::string representation = oss.str();` – Tony Delroy Feb 10 '15 at 03:07
  • I meant after switching to Mr Jerry's. Then the bin is returned as std::string. It's just that it does not include the msb zeros. I wanted to use an additional code to sort of pre-fill bin with necessary zeros. – Elnaz Feb 10 '15 at 03:22
  • @Elnaz: well, you can just say `while (bin.size() < width) bin = '0' + bin;` – Tony Delroy Feb 10 '15 at 03:45