Converting to binary value

Reply

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Converting to binary value

 
0
  #1
Oct 20th, 2008
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);
Last edited by kinger29; Oct 20th, 2008 at 11:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,360
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 240
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Converting to binary value

 
0
  #2
Oct 21st, 2008
"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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Converting to binary value

 
0
  #3
Oct 21st, 2008
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:
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:

  1. while(num>0)
  2. {
  3. temp = num%2; //obtain 0 or 1
  4. temp = mul * temp;// multiply with 1 or 10 or 100...
  5. sum+= temp; // sum holds the final value
  6. mul*=10; // modify mul and num for the next iteration
  7. num/=2;
  8. }
Last edited by devnar; Oct 21st, 2008 at 12:26 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC