Thread: Displaying Binary Numbers

  1. #1
    Registered User msummers's Avatar
    Join Date
    Apr 2008
    Location
    Southern California
    Posts
    11

    Displaying Binary Numbers

    Does anyone know how to display the binary number system in the output of a program?

    Any help would be greatly appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Did you try searching the forum first? This is a fairly common question.
    My best code is written with the delete key.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If you happen to have any code that would be great. As Prelude suggested there are numerous ways to show binary data, you will just need to search for those and apply the ones that are best suited for your application, be it a binary data file or convertion to binary and back.

  4. #4
    Registered User msummers's Avatar
    Join Date
    Apr 2008
    Location
    Southern California
    Posts
    11
    Code:
      #include <stdio.h>
      int main(){
      int i, val = 1;
      for(i = 1, i < 35, ++i){
      printf("%15d%15u%15x%15o\n", val, val, val, val);
      val *= 2;
      }
      return 0;
      }
    This is what my assignment is, but I would like to go the extra mile and incorporate the binary display if I do an incremental val count in the loop

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Newbie: "How do I print a number in binary?"
    Guru: "Search the forum, this is a common question."
    Newbie: "I want to print a number in binary."
    Guru: "Search the forum, this has been asked before."
    Newbie: "So how can I print a number in binary?"
    Guru: "Search the freaking forum already!"
    Newbie: "What about printing a number in binary?"

    * Guru beats Newbie to death with a blunt object *
    My best code is written with the delete key.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well your for loop needs to use ; and not a comma separator. Also your fourth val value in your printf statement should be renamed, and thus should be the output from another function that converts your decimal to binary, perhaps using a pointer to char to store your ones and zeros.

    What I mean is that you will need to call another function from within your for loop that will convert your 'val' value to binary. You will need to pass this value( in decimal if you like) and assign the output(char*) from this new conversion function to another value, call it binary.

    example

    Code:
    :
      printf("%15d%15u%15x%15s\n", val, val, val, binary);
      val *= 2
      dec_to_bin(val);
    }
      return 0;
    }
    
    char * dec_to_bin(int value)
    {
      char * value;
      :
        your code to convert;
      :
    
       return value;
    }
    As noted there are many functions out there and many ways to accomplish this. But this should get you going.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Sorry, you will need to define binary. Suggest


    Code:
    char * binary;
    Then

    Code:
    binary = dec_to_binary(int val);  /* example only */

  8. #8
    Registered User msummers's Avatar
    Join Date
    Apr 2008
    Location
    Southern California
    Posts
    11
    thank you

  9. #9
    Registered User msummers's Avatar
    Join Date
    Apr 2008
    Location
    Southern California
    Posts
    11

    Talking

    Quote Originally Posted by Prelude View Post
    Did you try searching the forum first? This is a fairly common question.
    Yes I searched first, and what I was looking for wasn't listed

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yes I searched first, and what I was looking for wasn't listed
    Ooh, that's such a lie. I've personally answered this question countless times, so you can't get away with telling me it's not there.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer displaying crazy numbers issue
    By Razorblade Kiss in forum C++ Programming
    Replies: 16
    Last Post: 12-20-2004, 11:00 PM
  2. Print binary numbers to disk file, problem
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 10-04-2004, 07:33 AM
  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. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  5. Visual C++ 6.0 - displaying line numbers
    By voltson in forum Tech Board
    Replies: 2
    Last Post: 11-09-2002, 09:13 PM