Thread: Decimal to binary but doesn't work

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Post [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 :

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int thisisanumber;
    
      cout<<"Please enter a number: ";
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered but in decimal!: "<< setbase(2) << thisisanumber;"\n";
      cin.get();
    }
    Last edited by southparklover; 07-18-2005 at 05:05 PM. Reason: I edited 'cause i got no reply

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Mmmm... I really need this, can someone help, please!

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I was about to help you, but if you can't wait more than 26 minutes without complaining, then no.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    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 += ' ';
    [edit]
    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]

    [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]

    [edit]
    code
    [/edit]
    Last edited by JoshR; 07-18-2005 at 08:00 PM.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    <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:
    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)
    ios_base::fmtflags
    typedef T1 fmtflags;
    static const fmtflags boolalpha, dec, fixed, hex,
    internal, left, oct, right, scientific,
    showbase, showpoint, showpos, skipws, unitbuf,
    uppercase, adjustfield, basefield, floatfield;

    Mmmm... I really need this, can someone help, please!
    southparklover,
    this is a bulletin board / forum... NOT a chat-room.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string changeBase(int number);
    
    int main()
    {
            int thisisanumber;
    
            cout<<"Please enter a number: ";
            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;
    }
    Last edited by major_small; 07-18-2005 at 07:50 PM. Reason: trigger-happy syntax highlighter :-(
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    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

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    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 .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Reading in Binary, Spitting out Decimal
    By neandrake in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 06:40 PM
  3. binary - decimal
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2003, 02:25 PM
  4. decimal to binary
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 07-10-2003, 12:03 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM