| | |
Converting to binary value
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 1
I am trying to convert an int, a char, and a short into their binary values and display it.
int a;
char b;
short c;
I understand how to convert an integer to a binary value bit by bit, I just don't understand how to store it as a varible so I can print it out as a binary value in a printf() line.
do{
int temp = a%2; //the value of the bit starting at the least significant
a = a/2;
//how would i store this bit by bit
}while(a>0);
int a;
char b;
short c;
I understand how to convert an integer to a binary value bit by bit, I just don't understand how to store it as a varible so I can print it out as a binary value in a printf() line.
do{
int temp = a%2; //the value of the bit starting at the least significant
a = a/2;
//how would i store this bit by bit
}while(a>0);
Last edited by kinger29; Oct 20th, 2008 at 11:41 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
The problem is to print the obtained remainders in the reverse order which implies that you'll have to store some sort of information abt what remainders you obtained in the previous step.
One way to do it is to use a character array as such:
The advantage of the above logic is that you can easily extend your code to make it convert to octal or hexadecimal just by doing:
Another way is to have a multiplier to multiply your obtained remainder with.
code snippet:
One way to do it is to use a character array as such:
char convert = "01"; Then you can use the remainder obtained as the index to the above string and store the resulting character in another array for later display.The advantage of the above logic is that you can easily extend your code to make it convert to octal or hexadecimal just by doing:
char convert="0123456789ABCDEF" . There recently was a thread which had this program. You can take a look at that.Another way is to have a multiplier to multiply your obtained remainder with.
code snippet:
c Syntax (Toggle Plain Text)
while(num>0) { temp = num%2; //obtain 0 or 1 temp = mul * temp;// multiply with 1 or 10 or 100... sum+= temp; // sum holds the final value mul*=10; // modify mul and num for the next iteration num/=2; }
Last edited by devnar; Oct 21st, 2008 at 12:26 am.
![]() |
Similar Threads
- converting binary to decimal (C)
- input to binary (C)
- Converting binary code to decimal value. (C++)
- Converting byte value into integer and vice versa (C++)
Other Threads in the C Forum
- Previous Thread: Passing Array Problemmm...
- Next Thread: about using data in structure from a text file
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






