Thread: Binary representation of numbers...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    Question Binary representation of numbers...

    Hello everyone. I am trying to write a program in C++ and I need to represent a series of numbers in binary form. I need numbers as big as 1,000,000 and I was wondering if C++ has some sort of function to change base 10 numbers into binary numbers?

    Thanks for the help in advance!

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

    bitset

    bitset is part of the Standard Template Library. You can use bitset for displaying numbers in binary. For input, you can use strtoul()

    Here's a program I wrote a few months ago that "converts" between bases.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I created a program that converts ASCII characters into integers (base 10) and binary (base 2) numbers... it won't help you much, except for maybe the binary function... I was just playing around with recursion on this program...

    Code:
    /*
    	Letter => Number => Binary
    
    	This Program will take an ASCII character and convert it into a number, then
    	convert it into Binary. The user may enter a single charater, or a string of
    	any length, seeing as the program uses a loop to convert it one charater at a
    	time.  The output is put into a file so it can easily be copied/pasted. The
    	file is overwritten every time the program runs.
    
                    Major_Small
    */
    
    #include <iostream.h>
    #include <fstream.h>
    
    int power(int),binary(int&,int);
    
    void main()
    {
    	char cha;
    	int i,ch,cho;
    	ofstream ofile("letnumbin.log",ios::trunc);
    
    	cout<<"Press Enter after you entered a string or ASCII\n"
    		<<"character to find The Decimal and Binary equivalent:\n\n";
    	while(cin.get(cha))
    	{
    		if(cha!='\n')
    		{
    			ch=int(cha);
    			cho=ch;	// assigns number to be sent to output file
    			cout<<cha<<'\t'<<ch<<'\t';
    			ofile<<cha<<'\t'<<ch<<'\t';
    			for(i=0;i<9;i++)
    			{
    				cout<<binary(ch,i);		// destroys int ch to 0 - cant be used ofile
    				ofile<<binary(cho,i);	// uses special assigned number
    			}
    			cout<<endl;
             ofile<<endl;
    		}
    	}
    	cout<<endl;
    }
    
    int binary(int& ch,int i)
    {
    	if(ch>=power(8-i))
    	{
    		ch-=power(8-i);
    		return 1;
    	}
    	else
       	return 0;
    }
    
    int power(int exponent)
    {
    	if(exponent>0)
    		return(2*power(exponent-1));
    	else
    		return 1;
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary representation in C
    By @nthony in forum C Programming
    Replies: 25
    Last Post: 11-10-2007, 12:43 AM
  2. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  3. something about binary numbers in c
    By louis_mine in forum C Programming
    Replies: 1
    Last Post: 02-09-2005, 10:22 PM
  4. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 05:46 PM
  5. # of ones in Binary representation
    By Matrix01 in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 10:15 AM