# Decimal to binary but doesn't work

• 07-18-2005
southparklover
[B]decimal to binary but doesnt work[/B]
This program is supposed to ask you for a number (it's just a practice for me) and you press enter and it changes your number to binary but it doesn't what's wrong... I'm learning :D :

Code:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int thisisanumber;

cin>> thisisanumber;
cin.ignore();
cout<<"You entered but in decimal!: "<< setbase(2) << thisisanumber;"\n";
cin.get();
}

• 07-18-2005
southparklover
Mmmm... I really need this, can someone help, please!
• 07-18-2005
Rashakil Fol
I was about to help you, but if you can't wait more than 26 minutes without complaining, then no.
• 07-18-2005
JoshR
Heres a function I wrote that converts to a binary string, try it out and see if ya like it.
Code:

template <typename ChildType>
string ConvtoBin (ChildType eData)
{
string bits;
for (int a = sizeof(eData) * CHAR_BIT; a > 0; a--)
bits += ((eData >> a) & 1) + 48;
return bits;
}

Now if you wanted a space after each 4 bits, then you could add before the bits+= line:
Code:

if (a % 4 == 0) bits += ' ';

I've never really heard of a function/statement in iomanip or any other header that actually prints out a string of bits. This is the only solution I can think of.
[/edit]

This function is byte size independant so you dont need to worry about how many bits are in a byte or in your number etc...
[/edit]

code
[/edit]
• 07-18-2005
DougDbug
<bitset>
Code:

#include <bitset>
...
const int MAX_BITS = 8;    // Limit to one-byte in this example
...
cout <<  << bitset<MAX_BITS>(thisisanumber) << "\t Binary (LSB)" << endl;

setbase only works for octal, decimal, and hex. The following is from dinkumware.com:
Quote:

setbase
T3 setbase(int base);
The manipulator returns an object that, when extracted from or inserted into the stream str, calls str.setf(mask, ios_base::basefield), then returns str. Here, mask is determined as follows:

If base is 8, then mask is ios_base::oct
If base is 10, then mask is ios_base::dec
If base is 16, then mask is ios_base::hex
If base is any other value, then mask is ios_base::fmtflags(0)
Quote:

ios_base::fmtflags
typedef T1 fmtflags;
static const fmtflags boolalpha, dec, fixed, hex,
internal, left, oct, right, scientific,
showbase, showpoint, showpos, skipws, unitbuf,

Quote:

Mmmm... I really need this, can someone help, please!
southparklover,
this is a bulletin board / forum... NOT a chat-room.
• 07-18-2005
quzah
Quote:

Originally Posted by DougDbug
Code:

#include <bitset>
...
const int MAX_BITS = 8;    // Limit to one-byte in this example

Quote:

Originally Posted by JoshR
Code:

string bits;
for (int a = sizeof(eData) * 8; a > 0; a--)

A char does not have to be 8 bits. That's why you have CHAR_BIT.

Quzah.
• 07-18-2005
major_small
try this
Code:

#include <iostream>
#include <string>

using namespace std;

string changeBase(int number);

int main()
{
int thisisanumber;

cin>> thisisanumber;
cin.ignore();
cout<<"Binary: "<< changeBase(thisisanumber) << endl;
cin.get();
return 0;
}

string changeBase(int number)
{
int len;
char ch;
string out;

while(number>0)
{
out+=static_cast<char>(number%2)+'0';
number/=2;
}

len=out.length();

for(int i=0;i<=len;i++)
{
ch=out.at(i);
out.at(i)=out.at(--len);
out.at(len)=ch;
}

return out;
}

• 07-18-2005
JoshR
Quote:

Originally Posted by quzah
A char does not have to be 8 bits. That's why you have CHAR_BIT.

Quzah.

Thanks Quzah, I know bout byte not having to be 8 bits but didnt know about CHAR_BIT :)
• 07-18-2005
southparklover
Quote:

Originally Posted by Rashakil Fol
I was about to help you, but if you can't wait more than 26 minutes without complaining, then no.

Sorry :rolleyes: .