ASCII to BINARY, & VICA VERCA

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 11
Reputation: keir.whitlock is an unknown quantity at this point 
Solved Threads: 0
keir.whitlock's Avatar
keir.whitlock keir.whitlock is offline Offline
Newbie Poster

ASCII to BINARY, & VICA VERCA

 
0
  #1
Apr 14th, 2004
Can anyone help?

Does anyone know of a simple peice of code in c/c++ that will allow me to convert ASCII characters into their relevent binary code and back again?

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: ASCII to BINARY, & VICA VERCA

 
0
  #2
Apr 15th, 2004
Originally Posted by keir.whitlock
Can anyone help?

Does anyone know of a simple peice of code in c/c++ that will allow me to convert ASCII characters into their relevent binary code and back again?

Cheers.
well if you know how to get the binary equvilent for a number, take the ascii char and assign it to an int to get an interget value then convert that to binary, or use the atoi() method and convert the result from that.

to do the reverse, if you used the assigning tactic, take the binary, and get the char equvilent by assigning the interger value to the char. if you used the atoi method, just take the binary convert it to an int, and use the itoa mehtod

check out this site:
http://nickciske.com/tools/binary.php
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,036
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: ASCII to BINARY, & VICA VERCA

 
0
  #3
Apr 16th, 2004
To continue where BountyX left off, to convert from integer to binary, use what I call the "calculating change" method - because it reminds me of one of the first C++ programs I learned was given 200 cents, calculate the equivalent in the smallest number of coins.

i.e. is number larger than 128, if yes, then a 1 ... is what is left larger than 64, if no, then a 0 ... is what is left larger than 32 ... and, well, you get the idea
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: ASCII to BINARY, & VICA VERCA

 
0
  #4
Apr 16th, 2004
use the & and << operators. remember that a character is just a sequence of bits. u cant test for each bit and output the result.
  1. int main()
  2. {
  3. char x = 'a';
  4. int y;
  5.  
  6. // the value of 'a' in hex is 0x61, or 97 decimal
  7. // in binary, that would be: 0 1 1 0 0 0 0 1
  8. // or 64 + 32 + 1, or 2^7 + 2^6 + 2^0
  9. //
  10. //loop for number of bits in a character
  11. //this will print out bits in reverse order
  12. //least significant bit first
  13. for(y = 0; y < sizeof(char) * 8; y++)
  14. printf("%c ", ( x & (1 << y) ) ? '1' : '0' );
  15.  
  16. puts("");
  17. return 0;
  18. }
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