C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-14-2008, 11:22 AM   #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.
msummers is offline   Reply With Quote
Old 04-14-2008, 11:36 AM   #2
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,674
Did you try searching the forum first? This is a fairly common question.
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Old 04-14-2008, 01:54 PM   #3
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 586
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.
slingerland3g is offline   Reply With Quote
Old 04-14-2008, 02:37 PM   #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
msummers is offline   Reply With Quote
Old 04-14-2008, 02:56 PM   #5
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,674
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.
Prelude is offline   Reply With Quote
Old 04-14-2008, 03:02 PM   #6
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 586
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.
slingerland3g is offline   Reply With Quote
Old 04-14-2008, 03:04 PM   #7
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 586
Sorry, you will need to define binary. Suggest


Code:
char * binary;
Then

Code:
binary = dec_to_binary(int val);  /* example only */
slingerland3g is offline   Reply With Quote
Old 04-14-2008, 03:09 PM   #8
Registered User
 
msummers's Avatar
 
Join Date: Apr 2008
Location: Southern California
Posts: 11
thank you
msummers is offline   Reply With Quote
Old 04-14-2008, 03:10 PM   #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
msummers is offline   Reply With Quote
Old 04-14-2008, 06:01 PM   #10
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,674
>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.
Prelude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer displaying crazy numbers issue Razorblade Kiss C++ Programming 16 12-20-2004 11:00 PM
Print binary numbers to disk file, problem Guti14 C Programming 4 10-04-2004 07:33 AM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Binary representation of numbers... roc C++ Programming 2 05-14-2003 07:42 PM
Visual C++ 6.0 - displaying line numbers voltson Tech Board 2 11-09-2002 09:13 PM


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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