Advertisement
Guest User

Untitled

a guest
Jul 16th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #include <stdlib.h>
  5.  
  6. string BINARY = "NULL";
  7. string TempStr = "NULL";
  8. int Bina = 0;
  9. int y = 0;
  10.  
  11. int Conversion(string Bin)
  12. {
  13. // Start Loop cycleing though the input
  14. for(int x = (Bin.length()-1); x >= 0; x--){
  15.  
  16. TempStr = Bin[y]; // Put the current location being calculated into a string so it can be converted into a int
  17.  
  18.  
  19. // 16 8 4 2 1
  20. // for example 1 1 0 0 1 which is 25
  21. // Using 1 * 2^4, 1 * 2^3, 0 * 2^2, 0 x 2^1, 1 * 2^0
  22. // 16 + 8 + 0 + 0 + 1 = 25
  23. /* for some reason the result being printed from cout below are:
  24. 1 4 = 1 x 2^4 6
  25. 1 3 = 1 x 2^3 1
  26. 0 2 " 2
  27. 0 1 " 1
  28. 1 0 " 2
  29. */
  30.  
  31. // I have the value calulated added to Bina each cycle.
  32. Bina += (atoi(TempStr.c_str())) * (2^x); // converting the string from before into the integer for the 1 or 0, then * it to get it's value that it holds.
  33. cout << Bin[y] << " " << x << " " << ((atoi(TempStr.c_str()) * 2^x)) << "\n"; // Print out information for debugging purposes.
  34. // ^^ This part is just 1 x 2^4 ect.
  35. y++; // Increase y by one so the cycle though the Binary works in order.
  36.  
  37.  
  38. }
  39. return Bina;
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. while(true){
  46. // Reset data for loop
  47. TempStr = "0";
  48. y = 0;
  49. Bina = 0;
  50. // User input
  51. cin >> BINARY;
  52. // Being calculation
  53. cout << Conversion(BINARY) << "\n\n";
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement