943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 36892
  • C++ RSS
Apr 14th, 2004
1

ASCII to BINARY, & VICA VERCA

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keir.whitlock is offline Offline
11 posts
since Apr 2004
Apr 15th, 2004
0

Re: ASCII to BINARY, & VICA VERCA

Quote 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
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Apr 16th, 2004
0

Re: ASCII to BINARY, & VICA VERCA

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
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Apr 16th, 2004
0

Re: ASCII to BINARY, & VICA VERCA

use the & and << operators. remember that a character is just a sequence of bits. u cant test for each bit and output the result.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Error : opening file a second time for a read
Next Thread in C++ Forum Timeline: help for program involving switch loops and file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC