![]() |
| | #1 |
| Registered User Join Date: Apr 2008 Location: Southern California
Posts: 11
| Displaying Binary Numbers Any help would be greatly appreciated. |
| msummers is offline | |
| | #2 |
| Code Goddess 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 | |
| | #3 |
| Registered User 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 | |
| | #4 |
| Registered User 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;
}
|
| msummers is offline | |
| | #5 |
| Code Goddess 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 | |
| | #6 |
| Registered User 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;
}
|
| slingerland3g is offline | |
| | #7 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 586
| Sorry, you will need to define binary. Suggest Code: char * binary; Code: binary = dec_to_binary(int val); /* example only */ |
| slingerland3g is offline | |
| | #8 |
| Registered User Join Date: Apr 2008 Location: Southern California
Posts: 11
| thank you |
| msummers is offline | |
| | #9 |
| Registered User Join Date: Apr 2008 Location: Southern California
Posts: 11
| |
| msummers is offline | |
| | #10 |
| Code Goddess 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |