Thread: How to convert binary number to 8 bits

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    How to convert binary number to 8 bits

    Hye..good day everyone..i need some help here..i have converted decimal number to binary number..but i have a problem..could someone help me on how to make the binary number to 8 bits only...

    thank u for ur help~!!


    Code:
    #include <conio.h>
    #include <iostream>
    using namespace std;
    
    void convertBinary( int n ) {
    if( n<2 ) {
    cout<<"binary is--> "<<n;
    return;
    }
    convertBinary(n/2);
    cout<<n % 2;
    }
    
    int main(){
        int n;
        int x;
        
        cout<<"n:";
        cin>>n;
        convertBinary( n );
        
       getch();
       return 0; 
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) Read in an unsigned int, not a signed int.
    2) Truncate it to the range 0-255.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 03:19 AM
  2. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  3. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  4. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM