| | |
Converting to binary value
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators initialization input interest intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






