Little Unsure?

This is a discussion on Little Unsure? within the C Programming forums, part of the General Programming Boards category; I have finally found a code that converts decimal to binary. But however I hardly understand it and I think ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Little Unsure?

    I have finally found a code that converts decimal to binary. But however I hardly understand it and I think it would be better if I know how to do it for myself instead of using pre fabricated code all the time eh?
    I could adapt this code into mine if I understood it completely but I don't so I am goin to post it and hope for sum help. Thanks in advance.

    Code:
    #include <iostream.h> 
    #include <stdlib.h>
    int a,b; 
    int n=0; 
    int arr[100]; 
    
    void main() 
    { 
    
        cout <<"Decimal : "; 
        cin >>a; 
      
        while (a>0)//just to be sure a is a number
        { 
            n++;//add one to your array varibale I would have guessed you did this last.
            b=a/2; //divide the decimal by 2 and assign it to b
        
            if (a%2==0)//if that divsion = 0
            { 
                arr[n]=0; //assign 0 to your array variable
            } 
            else 
            { 
                arr[n]=a-(2*b); //if it doesn't = 0 subtract the awnser of 2 times b
            } // from the initial decimal not sure why???????????????????
        
    
            a=b;//unsure why this is here
    
        } 
      
        cout<<"Binary : ";// this section out puts the binary backwards????????????????
        for (n=n;n>0;n--) 
        { 
            cout <<arr[n]; 
        } 
     system("PAUSE");
    }
    I have commented the main places I don't understand.

  2. #2
    *
    Guest
    This code is a mess, and in some places wrong.

    Look back through the threads c-programming threads maybe up to a year. One of the old, ultra-knowledgeable posters, sayah, posted complete sourcecode to do this, as well as other things.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    The fact that Sayeh and * have the same IP address in no way detracts from the glowing endorsment

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    As if we didn't know * was Sayeh after a post or two anyway.

    But I do agree with the ultra-knowledgeable part.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >As if we didn't know * was Sayeh after a post or two anyway.

    hehe, I was wondering if anyone else had been thinking this same thing.

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    I personally like the "BitWise" way, though ultra-knowledgeable part of sayah (or *) is cool!

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208

    Searched

    I searched the forums for ultra-knowledgable and for binary conversion and never found any of the posts u were talking about

  8. #8
    Unregistered
    Guest

    another way

    Depending on if you care about the math or the final answer....

    #include <iostream>
    #include <sstream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    stringstream Stream;
    char CharRep[32];
    int IntRep;

    cout<<"Decimal: ";

    cin>>CharRep; // Read in as a character array

    // convert char[] to int: in c use strtol()
    Stream<<CharRep;
    Stream>>IntRep;

    // because I dont want to bother figuring out why
    // setiosflags(ios::hex) doesnt work with stringstream
    //
    sprintf(CharRep, "%x", IntRep);

    // Output it, using the cheat that hex maps to binary
    // very easily
    for ( int i = 0; i < 32; i++ )
    {
    switch ( CharRep[i] )
    {
    case '0': cout<<"0000";break;
    case '1': cout<<"0001";break;
    case '2': cout<<"0010";break;
    case '3': cout<<"0011";break;
    case '4': cout<<"0100";break;
    case '5': cout<<"0101";break;
    case '6': cout<<"0110";break;
    case '7': cout<<"0111";break;
    case '8': cout<<"1000";break;
    case '9': cout<<"1001";break;
    case 'a': cout<<"1010";break;
    case 'b': cout<<"1011";break;
    case 'c': cout<<"1100";break;
    case 'd': cout<<"1101";break;
    case 'e': cout<<"1110";break;
    case 'f': cout<<"1111";break;
    case '\0':cout<<"b"<<endl; exit(0);
    default:
    cout<<" Error!!!"<<endl;
    exit(1);
    }
    }

    return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    from the looks of things, you might want to learn how to use arrays, how to declare them, assign values, how to access each element.

    then, you might also want to learn how to use pointers because pointers and arrays are very closely related.

    and when you done with those ... you could consider taking a look at struct(structures).

    the suggestions can go on... but you can start with arrays

    all the best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to Forms/MFC. Unsure where to place the code.
    By Swerve in forum Windows Programming
    Replies: 1
    Last Post: 02-02-2009, 12:15 AM
  2. Unsure on how to approach this concurrency problem
    By osiris^ in forum C# Programming
    Replies: 3
    Last Post: 04-29-2008, 11:47 PM
  3. unsure about auto/smart pointers
    By l2u in forum C++ Programming
    Replies: 16
    Last Post: 07-13-2007, 12:55 PM
  4. Unsure of how to properly use extern "C"
    By INFERNO2K in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2005, 10:54 AM
  5. unsure
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-13-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21