Results 1 to 4 of 4
  1. #1
    1337/PWNR
    Programmer
    Emu's Avatar
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
         User Info     Contact     Gamer ID
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
    Gamer IDs

    Gamertag: SpidermaN EaT PSN ID: Shaq23Jordan

    Contact info:

    Youtube Channel: www.youtube.com/eemuohg Kik Username: ShaqGlizzy

    Post Converting decimal numbers to binary example

    Hello CL, working on a program that converts number system from binary all the way to octal. Had a few people ask me recently how would I go about doing dec -> binary. So here is a code snippet that shows exactly what I did.

    Just a reminder that all this code is setup to run at bigO(0), which is the fastest runtime you could possibly get. This code is also setup to run smoothly with my much larger program of twelve different conversions.
    Code:
    //fnc to convert decimal to binary
    void decToBinary() {
    	int temp[1024];
    	string converted[1024];
    
    	system("cls");
    	cout << "Input decimal value: ";
    	cin >> temp[0];
    	cout << endl;
    
    	//convert and store in the string converted
    	for (int i=0; i<1023; i++) {
    		temp[i+1]=temp[i]/2;
    		
    		if (temp[i]%2==0) {
    			converted[i]="0";
    		}
    		else {
    			converted[i]="1";
    		}
    	}
    
    	//start from the end and remove 0's
    	for (int j=1023; j>0; j--) {
    		if (converted[j]=="0") {
    			converted[j]="";
    		}
    		if (converted[j]=="1") {
    			break;
    		}
    	}
    
    	cout << "resultant: ";
    
    	//print the binary starting from the end, so you can read it from left to right instead of right to left
    	for (int j=1023; j>=0; j--) {
    		cout << converted[j];
    	}
    
    	cout << endl;
    	cout << endl;
    
    	system("pause");
    	system("cls");
    }

    0 Not allowed! Not allowed!
    Last edited by Emu; 10-14-2013 at 12:04 PM.

  2. #2
    Nintendo Section Moderator PvtRyan's Avatar

    Badge

    Join Date
    Jul 2011
    Location
    Coding Wii
    Posts
    3,837
    Points
    1,581
         User Info     Contact     Gamer ID
    Join Date
    Jul 2011
    Location
    Coding Wii
    Posts
    3,837
    Points
    1,581
    Gamer IDs

    PSN ID: gamer_pvtryan Steam ID: pvtryan_cl Wii Code: PvtRyan

    Contact info:

    Youtube Channel: http://www.youtube.com/xPvtRyanx

    Default

    Quote Originally Posted by Emu View Post
    Hello CL, working on a program that converts number system from binary all the way to octal. Had a few people ask me recently how would I go about doing dec -> binary. So here is a code snippet that shows exactly what I did.

    Just a reminder that all this code is setup to run at bigO(0), which is the fastest runtime you could possibly get. This code is also setup to run smoothly with my much larger program of twelve different conversions.
    Code:
    //fnc to convert decimal to binary
    void decToBinary() {
    int temp[1024];
    string converted[1024];
    
    system("cls");
    cout << "Input decimal value: ";
    cin >> temp[0];
    cout << endl;
    
    //convert and store in the string converted
    for (int i=0; i<1023; i++) {
    temp[i+1]=temp[i]/2;
    
    if (temp[i]%2==0) {
    converted[i]="0";
    }
    else {
    converted[i]="1";
    }
    }
    
    //start from the end and remove 0's
    for (int j=1023; j>0; j--) {
    if (converted[j]=="0") {
    converted[j]="";
    }
    if (converted[j]=="1") {
    break;
    }
    }
    
    cout << "resultant: ";
    
    //print the binary starting from the end, so you can read it from left to right instead of right to left
    for (int j=1023; j>=0; j--) {
    cout << converted[j];
    }
    
    cout << endl;
    cout << endl;
    
    system("pause");
    system("cls");
    }
    Not bad. Keep up the good work, you might inspire me to make a tut on binary.


    <PvtRyan used Tapatalk to make this Post>

    0 Not allowed! Not allowed!


    PS4 Profile:

  3. #3

    Retired Prophet
    ToxicJew.'s Avatar
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,809
    Points
    7,616
         User Info     Contact     Gamer ID
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,809
    Points
    7,616

    Contact info:

    Youtube Channel: http://www.youtube.com/kennethbgoodin

    Default

    Nice dude! I may make the same in Java, just as a practice thing.

    0 Not allowed! Not allowed!
    ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้ส็็็็็็็็็็็็็็็็็็็็ ็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้

  4. #4
    1337/PWNR
    Programmer
    Emu's Avatar
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
         User Info     Contact     Gamer ID
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
    Gamer IDs

    Gamertag: SpidermaN EaT PSN ID: Shaq23Jordan

    Contact info:

    Youtube Channel: www.youtube.com/eemuohg Kik Username: ShaqGlizzy

    Default

    No problem guys :P. I also want to suggest that you both do it, because it involves complex thinking because you have to think like the computer, and know how to convert yourself before you actually can tell the computer to do it for you. I only have all the decimal conversions completed so far, meaning I have 9 more to tackle.

    0 Not allowed! Not allowed!

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 04:23 PM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.