Binary converter

This is a discussion on Binary converter within the C++ Programming forums, part of the General Programming Boards category; I don't know if anyone has made on but this is kind of a brute force method... check it out. ...

  1. #1
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262

    Binary converter

    I don't know if anyone has made on but this is kind of a brute force method... check it out.

    Code:
    // Phillip Hermans
    // Feburary 18, 2003
    
    #include <iostream.h>
    
    
    int main()
    	{
    	int i,ent,x = 128,wot,q;
    	do{
    	cout << "Enter a number greater than 0 and less that 256: ";
    	cin >> ent;
    	}
    	while(ent > 256 || ent < 0);
    	wot = ent;
    
    	for(i = 1; i < 9; i++)
    		{
    		if(x > wot)
    			cout << "0";
    		else
    			{
    			cout << "1";
    			wot = wot % x;
    			}
    		x /= 2;
    		}
    
    	return 0;
    	}
    Like it?

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

    bitset

    There's a class called bitset. I think its part of the STL (standard template library). It's very easy to display a number in binary using bitset. (I have an example at home, and I could post it later.) There was a post a few weeks ago showing how to use it too. So, you might want to search.

    [edit]
    You'll need to include the header file bitset.h
    Last edited by DougDbug; 02-18-2003 at 03:21 PM.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This will display byte x in binary:
    Code:
    #include <bitset>
    cout << bitset<8>(x)
    The attached sample program uses various bases.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  2. Binary to text converter
    By serena in forum C Programming
    Replies: 2
    Last Post: 04-13-2005, 04:28 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 01:21 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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