Does anyone know how to display the binary number system in the output of a program?
Any help would be greatly appreciated.
Does anyone know how to display the binary number system in the output of a program?
Any help would be greatly appreciated.
Did you try searching the forum first? This is a fairly common question.
My best code is written with the delete key.
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.
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 loopCode:#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; }
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.
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
As noted there are many functions out there and many ways to accomplish this. But this should get you going.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; }
Sorry, you will need to define binary. Suggest
ThenCode:char * binary;
Code:binary = dec_to_binary(int val); /* example only */
>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.