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;