RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1331 | Replies: 4
Reply
Join Date: Jul 2006
Posts: 41
Reputation: jigoro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jigoro jigoro is offline Offline
Light Poster

Question input to binary

  #1  
Mar 21st, 2007
is there a way to convert everything i input to my program into its ascii binary equivalent? if so please show me; thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Solution Re: input to binary

  #2  
Mar 21st, 2007
I wish you had written the language which you are using...
I wrote it in C but you can easily integrate it on C++...

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6. char ch;//input character
  7. int ascii;//input's ascii equivalent
  8. int b,i;
  9.  
  10. double sum = 0;//You can initilaize it to int but some compilers(like Dev-C++) causes troubles and generates wrong output...
  11.  
  12. printf("Enter any character ");
  13. scanf(" %c",&ch);
  14.  
  15. ascii = ch;//if you asssign a char to an int, the int variable holds the assigned character's ASCII code
  16.  
  17.  
  18.  
  19. for(i = 0;; i++)
  20. {
  21. b = ascii % 2;//converting binary...
  22. ascii /= 2;//converting binary...
  23.  
  24. sum += b * pow(10,i);//variable b's first value is first digit the binary code from right to left
  25. //so we multiply it by E+0
  26. printf("%0.lf\n",sum);//Watch how your variable to be converted in ASCII
  27.  
  28. if(ascii == 1 || ascii == 0) break;//If we divide an integer to 2, we obtain 1 or 0 as a result
  29. //if we don't control this will be an infinite loop.
  30.  
  31.  
  32. }
  33.  
  34. sum += ascii * pow(10,i + 1);//End of our loop we divided the value 2 again and the variable ascii's value was 1
  35. //so we exit the loop beacause of the if statement and we didn't multiply it E + 6.We are doing this here.
  36.  
  37. printf("%0.lf",sum);//Output...
  38.  
  39.  
  40.  
  41. return 0;
  42.  
  43. }
Last edited by FoX_ : Mar 21st, 2007 at 10:32 am.
Reply With Quote  
Join Date: Jul 2006
Posts: 41
Reputation: jigoro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jigoro jigoro is offline Offline
Light Poster

Re: input to binary

  #3  
Mar 21st, 2007
im sorry for not putting the language. i program in c++, thanks for the code.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 1,289
Reputation: vishesh is on a distinguished road 
Rep Power: 5
Solved Threads: 32
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: input to binary

  #4  
Mar 21st, 2007
Well, you must post whatever you have tried till now. See this
But since, you have got solution, you may see this too:

http://www.daniweb.com/code/snippet656.html
Reply With Quote  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: input to binary

  #5  
Mar 21st, 2007
Originally Posted by jigoro View Post
im sorry for not putting the language. i program in c++, thanks for the code.


Not that it matters...
As I told my previous message , you can easily integrate this code on C++...
Last edited by FoX_ : Mar 21st, 2007 at 12:46 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:56 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC